# 4SpringMVC数据绑定
# 导航
什么是SpringMVC中的数据绑定?
就是Spring MVC中的Controll控制器的某一个方法是如何和url绑定在一起的
# spring MVC中URL Mapping(URL映射)
- URL Mapping指将URL与Controller方法绑定
- 通过将URL与方法绑定,Spring MVC便可通过Tomcat对外暴露服务
# URL Mapping注解
- @RequestMapping - 通用绑定
- @GetMapping - 绑定Get请求
- @PostMapping - 绑定Post请求
# 接收请求参数
接收请求参数的常用做法
- 使用Controller方法参数接收
- 使用Java Bean接收数据
# 使用Controller方法参数接收
post接收参数:伪代码如下:
<form action="/p" method="post">
<input name="username" type="text">
<input name="password" type="password">
<input type="submit" value="提交">
</form>
1
2
3
4
5
2
3
4
5
package com.torey.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/5/4 15:47
* @描述:
*/
@Controller
public class URLMappingController {
@PostMapping("/p")
@ResponseBody
public String postMapping(String username,Long password){
System.out.println(username + ":" + password);
return "This is postMapping!"+username + ":" + password;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
get方式请求参数
//http://localhost/g?user_name=liLiy
@GetMapping("/g")
@ResponseBody
public String getMapping(@RequestParam("user_name") String userName){
return "This is getMapping!!"+userName;
}
1
2
3
4
5
6
2
3
4
5
6
# 使用Java Bean接收请求参数
<form action="/p1" method="post">
<input name="username" type="text">
<input name="password" type="password">
<input type="submit" value="提交">
</form>
1
2
3
4
5
2
3
4
5
@PostMapping("/p1")
@ResponseBody
public String postUserMapping(UserDto userDto){
System.out.println(userDto.toString());
return "This is p1!"+userDto.toString();
}
1
2
3
4
5
6
2
3
4
5
6
@Data
public class UserDto {
private String username;
private Long password;
}
1
2
3
4
5
2
3
4
5
# 综合训练:学员调查问卷
- 利用数组或者List接收请求中的复合数据
- 利用@RequestParam为参数设置默认值
- 使用Map对象接收请求参数及注意事项
# URL绝对路径与相对路径
# 数组参数接收
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>学员调查问卷</title>
<style>
.container {
position: absolute;
border: 1px solid #cccccc;
left: 50%;
top: 50%;
width: 400px;
height: 300px;
margin-left: -200px;
margin-top: -150px;
box-sizing: border-box;
padding: 10px;
}
h2{
margin: 10px 0px;
text-align: center;
}
h3{
margin: 10px 0px;
}
</style>
</head>
<body>
<div class="container">
<h2>学员调查问卷</h2>
<form action="./apply" method="post">
<h3>您的姓名</h3>
<input name="name" class="text" style="width: 150px">
<h3>您正在学习的技术方向</h3>
<select name="course" style="width: 150px">
<option value="java">Java</option>
<option value="h5">HTML5</option>
<option value="python">Python</option>
<option value="php">PHP</option>
</select>
<div>
<h3>您的学习目的:</h3>
<input type="checkbox" name="purpose" value="1">就业找工作
<input type="checkbox" name="purpose" value="2">工作要求
<input type="checkbox" name="purpose" value="3">兴趣爱好
<input type="checkbox" name="purpose" value="4">其他
</div>
<div style="text-align: center;padding-top:10px" >
<input type="submit" value="提交" style="width:100px">
</div>
</form>
</div>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.torey.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/5/4 18:28
* @描述:
*/
@Controller
public class ApplyController {
@PostMapping("apply")
@ResponseBody
public String apply(@RequestParam(value = "name",defaultValue = "默认值") String name, String course, Integer[] purpose){
System.out.println(name);
System.out.println(course);
for (Integer integer : purpose) {
System.out.println(integer);
}
return "success";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# list参数接收
@PostMapping("apply2")
@ResponseBody
public String apply2(@RequestParam(value = "name",defaultValue = "默认值") String name, String course, @RequestParam List<Integer> purpose){
System.out.println(name);
System.out.println(course);
for (Integer integer : purpose) {
System.out.println(integer);
}
return "success";
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 实体类接收参数
package com.torey.springmvc.dto;
import lombok.Data;
import java.util.List;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/5/4 19:07
* @描述:
*/
@Data
public class FormDto {
String name;
String course;
List<Integer> purpose;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
实体类接收
@PostMapping("apply")
@ResponseBody
public String apply(FormDto formDto){
System.out.println(formDto.getName());
System.out.println(formDto.getCourse());
for (Integer integer : formDto.getPurpose()) {
System.out.println(integer);
}
return "success";
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
map接收
/**
* map 只适合但数据的表单,不适合有符合数据(数组数据),一般不建议使用map
* @param map
* @return
*/
@PostMapping("apply")
@ResponseBody
public String apply(@RequestParam Map<String,Object> map){
System.out.println(map);
return "success";
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 关联对象赋值
<form action="./apply" method="post">
<h3>用户名</h3>
<input name="username" class="text" style="width: 150px">
<h3>密码</h3>
<input name="password" class="text" style="width: 150px">
<h3>姓名</h3>
<input name="idcard.name" class="text" style="width: 150px">
<h3>身份证</h3>
<input name="idcard.idno" class="text" style="width: 150px">
<div style="text-align: center;padding-top:10px" >
<input type="submit" value="提交" style="width:100px">
</div>
</form>
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
package com.torey.springmvc.dto;
import lombok.Data;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/5/5 10:17
* @描述:
*/
@Data
public class IdcardDto {
private String name;
private String idno;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.torey.springmvc.dto;
import lombok.Data;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/5/4 18:08
* @描述:
*/
@Data
public class UserDto {
private String username;
private Long password;
private IdcardDto idcard;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.torey.springmvc.controller;
import com.torey.springmvc.dto.FormDto;
import com.torey.springmvc.dto.UserDto;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Map;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/5/4 18:28
* @描述:
*/
@Controller
public class ApplyController {
@PostMapping("apply")
@ResponseBody
public String apply(UserDto userDto){
System.out.println(userDto);
return "success";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 日期参数的处理
如何接受日期类型的数据? 答:在实体类 或者方法参数上加 @DateTimeFormat(pattern = "yyyy-MM-dd")即可
# 在实体类 或者方法参数上加@DateTimeFormat(pattern = "yyyy-MM-dd")
package com.torey.springmvc.dto;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/5/5 10:17
* @描述:
*/
@Data
public class IdcardDto {
private String name;
private String idno;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date expire;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@PostMapping("apply")
@ResponseBody
public String apply(UserDto userDto,@DateTimeFormat(pattern = "yyyy-MM-dd") Date createTime){
System.out.println(userDto);
System.out.println(createTime);
return "success";
}
1
2
3
4
5
6
7
2
3
4
5
6
7
<form action="./apply" method="post">
<input name="idcard.idno" class="text" style="width: 150px">
<h3>过期时间</h3>
<input name="idcard.expire" type="date" style="width: 150px">
<h3>创建时间</h3>
<input name="createTime" type="date" style="width: 150px">
<div style="text-align: center;padding-top:10px" >
<input type="submit" value="提交" style="width:100px">
</div>
</form>
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 如何设置全局日期格式
- 需要新建一个 转换日期类,继承 Converter 接口(org.springframework.core.convert.converter.Converter)
- 在applicationContext.xml中配置,让spring mvc知道这是一个日期转换类
- <mvc:annotation-driven conversion-service="conversionService"/>
package com.torey.springmvc.converter;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/5/5 11:05
* @描述:
*/
public class MyDateConverter implements Converter<String,Date> {
public Date convert(String s) {
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(s);
} catch (ParseException e) {
return null;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
在applicationContext.xml中配置,让spring mvc知道这是一个日期转换类
<mvc:annotation-driven conversion-service="converterServer"/>
<bean id="converterServer" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.torey.springmvc.converter.MyDateConverter"/>
</set>
</property>
</bean>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
applicationContext.xml所有代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mv="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--
context:component-scan 标签的作用:
在Spring IOC初始化过程中,自动创建并管理com.torey.springmvc包及子包中拥有以下注解的对象:
@Repository 、@Service 、@Controller 、@Component
-->
<context:component-scan base-package="com.torey.springmvc"></context:component-scan>
<!--启用Spring MVC的注解开发模式-->
<mvc:annotation-driven conversion-service="converterServer"/>
<!--将图片/JS/CSS等静态资源排除在外,可提高执行效率-->
<mvc:default-servlet-handler/>
<!--转换类 id可以随便写;FormattingConversionServiceFactoryBean 就是用来通知spring mvc到底有哪些自定义的转换类;配置好后需要与 <mvc:annotation-driven conversion-service="converterServer"/>关联起来-->
<bean id="converterServer" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.torey.springmvc.converter.MyDateConverter"/>
</set>
</property>
</bean>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
这时候使用的时候,就参数或者类变量成员上不需要写@DateTimeFormat(pattern = "yyyy-MM-dd")
如果即配置了全局日期转换器,同时参数或者类成员变量上也配置了@DateTimeFormat(pattern = "yyyy-MM-dd"),这时候是以全局日期转换器为准,所以不建议同时配置两个
# 测试文件目录如下
# 导航,上一页,下一页
3springMVC环境配置
5springMVC请求中文乱码问题
# 支持我-微信扫一扫-加入微信公众号
# 赞赏作者
# 种一棵树,最好的时间是十年前,其次是现在
立志用功,如种树然。方其根芽,犹未有干;及其有干,尚未有枝;枝而后叶,叶而后花、实。初种根时,只管栽培灌溉,勿作枝想,勿作实想。悬想何益?何不忘栽培之功,怕没有枝叶花实?