请求参数的绑定说明
1. 绑定机制
①. 表单提交的数据都是k=v格式的 username=haha&password=123
②. SpringMVC的参数绑定过程是把表单提交的请求参数,作为控制器中方法的参数进行绑定的
③. 要求:提交表单的name和参数的名称是相同的
2. 支持的数据类型
????????①. 基本数据类型和字符串类型
????????②. 实体类型(JavaBean)
????????③. 集合数据类型(List、map集合等)
????????基本数据类型和字符串类型
????????????????①. 提交表单的name和参数的名称是相同的
????????????????②. 区分大小写
????????实体类型(JavaBean)
????????????????①. 提交表单的name和JavaBean中的属性名称需要一致
????????????????②. 如果一个JavaBean类中包含其他的引用类型,那么表单的name属性需要编写成:对象.属性 例如:address.name
????????????????给集合属性数据封装
? ? ? ? ? ? ? ? ①. JSP页面编写方式:list[0].属性
index.html代码
<html>
<head>
<meta charset="utf-8">
<title>请求参数的绑定</title>
</head>
<body>
<form action="/SpringMVCDemo/my/sever1.do" method="get">
<input type="text" name="username">
<input type="text" name="password">
<input type="submit" value="get提交">
</form>
<form action="/SpringMVCDemo/my/sever2.do" method="post">
<input type="text" name="username">
<input type="text" name="password">
<input type="submit" value="post提交">
</form>
<form action="/SpringMVCDemo/my/sever5.do" method="get">
<input type="text" name="username"/>
<input type="text" name="password"/>
<input type="submit" value="封装到实体类提交"/>
</form>
<form action="/SpringMVCDemo/my/sever5.do" method="get">
<input type="text" name="username"/>
<input type="text" name="password"/>
<input type="text" name="account.money"/>
<input type="text" name="car.money"/>
<input type="submit" value="提交"/>
</form>
<form action="/SpringMVCDemo/my/sever5.do" method="get">
<input type="text" name="username"/>
<input type="text" name="password"/>
<input type="text" name="account.money"/>
<input type="text" name="car.money"/>
<input type="text" name="accounts[0].money"/>
<input type="text" name="accounts[1].money"/>
<input type="text" name="accounts[2].money"/>
<input type="submit" value="存在list集合提交"/>
</form>
</body>
</html>
?Account代码
package com.qcby.entity;
public class Account {
private Double money;
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"money=" + money +
'}';
}
}
Car代码
package com.qcby.entity;
public class Car {
private Double money;
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "Car{" +
"money=" + money +
'}';
}
}
User代码
package com.qcby.entity;
import java.util.List;
public class User {
private String username;
private String password;
private Account account;
private Car car;
List<Account> accounts;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public List<Account> getAccounts() {
return accounts;
}
public void setAccounts(List<Account> accounts) {
this.accounts = accounts;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", account=" + account +
", car=" + car +
", accounts=" + accounts +
'}';
}
}
MyController代码
package com.qcby.Controller;
import com.qcby.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("my")
public class MyController {
@RequestMapping(path = "/sever1.do",method ={RequestMethod.GET})
public void save1(String username,String password){
System.out.println(username);
System.out.println(password);
}
@RequestMapping(path = "/sever2.do",method ={RequestMethod.POST})
public void save2(String username,String password){
System.out.println(username);
System.out.println(password);
}
@RequestMapping(path = "/sever3",method ={RequestMethod.GET})
public void save3(User user){
System.out.println(user.toString());
}
@RequestMapping(path = "/sever4",method ={RequestMethod.GET})
public void save4(User user){
System.out.println(user.toString());
}
@RequestMapping(path = "/sever5",method ={RequestMethod.GET})
public void save5(User user){
System.out.println(user.toString());
}
}
启动Tomcat,运行测试
post请求中参数中文乱码的解决
在web.xml中配置Spring提供的过滤器类
<!-- 配置过滤器,解决中文乱码的问题-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在控制器中使用原生的ServletAPI对象
1. 只需要在控制器的方法参数定义HttpServletRequest和HttpServletResponse对象
@RequestMapping("/save6.do")
public String save6(HttpServletRequest request, HttpServletResponse response){
System.out.println(request);
// 获取到HttpSession对象
HttpSession session = request.getSession();
System.out.println(session);
System.out.println(response);
return "suc";
}
|