支持的数据类型
基本数据类型(包括String类型),pojo类型(实体类以及关联的实体类),数组和集合类型
使用要求
- 基本数据类型和String类型
要去参数名称必须和控制器的方法形参必须一致,区分大小写。 - pojo类型
要求表单中参数名称和 POJO 类的属性名称保持一致。并且控制器方法的参数类型是 POJO 类型。 - 数组和集合类型
第一种:
要求集合类型的请求参数必须在 POJO 中。在表单中请求参数名称要和 POJO 中集合属性名称相同。给 List 集合中的元素赋值,使用下标。给 Map 集合中的元素赋值,使用键值对。
第二种:
接收的请求参数是 json 格式数据。需要借助一个注解实现。
实例:基本数据类型和String类型
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>springmvc参数绑定</title>
</head>
<style>
h3{
text-align: center;
}
</style>
<body>
<h3>
<a href="${pageContext.request.contextPath}/user/p0?name=zh&age=20&money=1000">点击进入</a><br>
</h3>
</body>
</html>
@Controller
@RequestMapping(path = "/user")
public class paramController {
/***
* @Description 基本数据类型和String数据类型
* @Param: [name, age, money]
* @return: java.lang.String
*/
@RequestMapping(path = "/p0", method = RequestMethod.GET)
public String param1(String name, Integer age, Double money){
System.out.println("姓名:" + name + "年龄:" + age + "钱:" + money);
return "success";
}
}
实例:pojo类型
//用户实体类
public class User implements Serializable {
//基本属性
private String username;
private String password;
private Integer age;
//引用实体类属性
private Address address;
//别忘记getxxx/setxxx/toString()方法
}
//地址实体类
public class Address implements Serializable {
//城市名称
private String cityAddress;
//详细地址
private String detailedAddress;
//别忘记getxxx/setxxx/toString()方法
}
jsp页面准备一个表单
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>springmvc参数绑定学习</title>
</head>
<style>
h3 {
text-align: center;
}
</style>
<body>
<h3>
<form action="${pageContext.request.contextPath}/user/p1" method="post"><br>
<label>用户名:</label>
<input type="text" name="username"><br>
<label>密码:</label>
<input type="password" name="password"><br>
<label>年龄:</label>
<input type="text" name="age"><br>
<label>城市名称:</label>
<input type="text" name="address.cityAddress"><br>
<label>详细地址:</label>
<input type="text" name="address.detailedAddress"><br>
<input type="submit" value="提交">
</form>
</h3>
</body>
</html>
控制器代码:
@Controller
@RequestMapping(path = "/user")
public class paramController {
/***
* @Description 请求参数时实体类
* @Param: [name, age, money]
* @return: java.lang.String
*/
@RequestMapping(path = "/p1", method = RequestMethod.POST)
public String param2(User user){
System.out.println(user);
return "success";
}
}
实例:pojo中包含集合类型
//用户实体类中添加集合属性
public class User implements Serializable {
//基本属性
private String username;
private String password;
private Integer age;
//list集合
private List<Address> list;
//map集合
private Map<String, Address> map;
//别忘记getxxx/setxxx/toString()方法
}
jsp代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>springmvc参数绑定学习</title>
</head>
<style>
h3 {
text-align: center;
}
</style>
<body>
<h3>
<form action="${pageContext.request.contextPath}/user/p1" method="post"><br>
<label>用户名:</label>
<input type="text" name="username"><br>
<label>密码:</label>
<input type="password" name="password"><br>
<label>年龄:</label>
<input type="text" name="age"><br>
<label>城市名称1:</label>
<input type="text" name="list[0].cityAddress"><br>
<label>详细地址1:</label>
<input type="text" name="list[0].detailedAddress"><br>
<label>城市名称2:</label>
<input type="text" name="map[c].cityAddress"><br>
<label>详细地址2:</label>
<input type="text" name="map[c].detailedAddress"><br>
<input type="submit" value="提交">
</form>
</h3>
</body>
</html>
控制器代码:
@Controller
@RequestMapping(path = "/user")
public class paramController {
@RequestMapping(path = "/p1", method = RequestMethod.POST)
public String param2(User user){
System.out.println(user);
return "success";
}
}
解决post请求参数乱码
在web.xml配置一个过滤器
<!--解决post请求中文乱码-->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!--过滤所有请求-->
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>