springmvc03 接收请求参数
作者:互联网
3.1 基本类型参数
请求参数和方法的形参 同名即可
springMVC默认可以识别的日期字符串格式为: YYYY/MM/dd HH:mm:ss
通过@DateTimeFormat可以修改默认日志格式
//网页端输入: http://xxxx/param/test1?id=1&name=shine&gender=true&birth=2020/12/12 12:13:20
@RequestMapping("/test1")
public String test1(Integer id, String name, Boolean gender, Date birth){
System.out.println("test1");
System.out.println("id:"+id+" name:"+name+" gender:"+gender+" birth:"+birth);
return "hello";
}
http://localhost:8080/param/test1?id=1&name=shine&gender=true&birth=2020/12/12%2012:13:20
test1
id:1 name:shine gender:true birth:Sat Dec 12 12:13:20 CST 2020
3.2 实体收参【重点
】
请求参数和实体的属性 同名即可
上述使用参数办法如果参数较多不方便,就创建一个实体类承载这些参数
public class User {
private Integer id;
private String name;
private Boolean gender;
private Date birth;
private String[] hobby;
//set/get ...
}
// http://xxxx/param/test1?id=1&name=shine&gender=true&birth=2020/12/12 12:13:20
@RequestMapping("/test2")
public String test2(User user){
System.out.println("test2");
System.out.println(user);
return "hello";
}
http://localhost:8080/param/test1?id=1&name=shine&gender=true&birth=2020/12/12%2012:13:20
test1
id:1 name:shine gender:true birth:Sat Dec 12 12:13:20 CST 2020
3.3 数组收参
简单类型的 数组
创建表单jsp
<form action="${pageContext.request.contextPath}/param/test3">
id: <input type="text" name="id"> <br>
name:<input type="text" name="name"><br>
gender:<input type="text" name="gender"><br>
birth:<input type="text" name="birth"><br>
<input type="checkbox" name="hobby" value="football"> 足球
<input type="checkbox" name="hobby" value="basketball"> 篮球
<input type="checkbox" name="hobby" value="volleyball"> 排球 <br>
<input type="submit" value="提交">
</form>
// http://xxxx/param/test3?hobby=football&hobby=basketball&hobby=volleyball
@RequestMapping("/test3")
// public String test3(Stirng[] hobby){
public String test3(User user){
System.out.println("test3");
System.out.println(user);
return "hello";
}
http://localhost:8080/param.jsp
http://localhost:8080/param/test3?id=1&name=xiaoming&gender=true&birth=2020%2F12%2F20&hobby=football&hobby=basketball
test3
User{id=1, name='xiaoming', gender=true, birth=Sun Dec 20 00:00:00 CST 2020, hobby=[football, basketball]}
3.4 集合收参 【了解】
注意:如果使用集合地址栏输入集合的【】符号时会乱码,需要用表单传递参数
public class UserList {
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
private List<User> users;
}
<form action="${pageContext.request.contextPath}/param/test4" method="post">
id: <input type="text" name="users[0].id"> <br>
name:<input type="text" name="users[0].name"><br>
gender:<input type="text" name="users[0].gender"><br>
<hr>
id: <input type="text" name="users[1].id"> <br>
name:<input type="text" name="users[1].name"><br>
gender:<input type="text" name="users[1].gender"><br>
<input type="submit" value="提交">
</form>
// post请求:http://...?users[0].id=1&users[0].name=zhangsan&users[0].birth=2018-12-12&users[1].id=2&....
// http://xxx/param/test4?users[0].id=1&users[0].name=shine&users[0].gender=true&users[1].id=2&users[1].name=zhangsan
@RequestMapping("/test4")
public String test4(UserList userList){
System.out.println("test4");
for (User user : userList.getUsers()) {
System.out.println(user);
}
return "hello";
}
3.5 路径参数
// {id} 定义名为id的路径;【/hello/{id}】的匹配能力和【/hello/*】等价
// http://localhost:8989/.../hello/10 {id}匹配到10
// {id} 命名路径
// {id} 等价于* 后面可以写任意符合类型的id 如:/test5/1 test5/2 test5/xxxx
@RequestMapping("/test5/{id}")
public String test5(@PathVariable("id") Integer id){
System.out.println("test5");
System.out.println("id:"+id);
return "hello";
}
@RequestMapping("/test6/{id}/{name}")
public String test6(@PathVariable Integer id,@PathVariable("name") String name2){//把{id} 路径匹配到的值赋给name参数
System.out.println("test6");
System.out.println("id:"+id);
System.out.println("name:"+name2);
return "hello";
}
http://localhost:8080/param/test5/22
test5
id:22
http://localhost:8080/param/test6/22/xiaoming
test6
id:22
name:xiaoming
3.6 中文乱码
首先,页面中字符集统一
JSP : <%@page pageEncoding="utf-8" %>
HTML : <meta charset="UTF-8">
其次,tomcat中字符集设置,对get请求中,中文参数乱码有效
Tomcat配置:URIEncoding=utf-8
最后,设置此filter,对post请求中,中文参数乱码有效,
在web.xml中配置过滤器
<!-- 此过滤器会进行:request.setCharactorEncoding("utf-8"); -->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
标签:springmvc03,12,请求,gender,birth,users,接收,id,name 来源: https://blog.csdn.net/weixin_44688581/article/details/116427716