0%

SSM-10-数据绑定

SSM-10-数据绑定

将请求消息数据与后台方法参数建立连接的过程就叫数据绑定

简单数据绑定

绑定默认数据类型

1
2
3
4
5
6
7
8
9
10
11
12
13

@Controller
public class UserController {
@RequestMapping("/selectUser")
/* 默认支持的数据类型
* HttpServletResponse response, HttpSession session, Model model, ModelMap modelmap
*/
public String selectUser(HttpServletRequest request) {
String id = request.getParameter("id");
System.out.println("id=" + id);
return "success";
}
}

就是前端如何传参

  1. 创建Web项目
  2. 导入相应jar包
  3. 编写Spring-config配置文件
  4. 编写Controller控制
  5. 生成jsp

绑定简单数据类型

就是java基本数据类型绑定

直接使用

1
2
3
4
5
6
7
8
@Controller
public class UserController {
@RequestMapping("/selectUser")
public String selectUser(Integer id) {
System.out.println("id=" + id);
return "success";
}
}

@RequestParam

value name属性的别名

1
2
3
4
5
6
7
8
@Controller
public class UserController {
@RequestMapping("/selectUser")
public String selectUser(@RequestParam(value="user_id")Integer id) {
System.out.println("id=" + id);
return "success";
}
}

可以加个别名传参,这样同时还有默认值,Required必须

绑定POJO类型

先创建User

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
package com.itheima.po;

/**
* @author NoOne dotaerday@gmail.com:
* @version 创建时间:2020年3月9日 下午11:31:26 类说明
*/
public class User {
private Integer id;
private String username;
private String password;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

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;
}


}

Controller设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
* 向用户注册页面跳转
*/
@RequestMapping("/toRegister")
public String toRegister() {
return "register";
}
/*
* 接受用户信息
*/
@RequestMapping("/registerUser")
public String registerUser(User user) {
String username = user.getUsername();
String password = user.getPassword();
System.out.println(username);
System.out.println(password);
return "success";
}

这里toRegister就跳转到register.jsp去,然后register.jsp通过post请求到registerUser,拿到数据解析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/registerUser" method="post">
用户名:<input type="text" name="username" /> </br>
密码:<input type="text" name="password"/> </br>
<input type="submit" value="注册"/>
</form>
</body>
</html>

这里注意中文乱码,web.xml配置编码过滤器

1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- 配置编码过滤器 -->
<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>

这个实际就是通过限定post数据内容,来传递参数的,通过对浏览器抓包,发觉他就是传了username和password

绑定包装POJO

这种其实就是对象嵌套对象,问题不大,抓包查看

1
ordersId=123&user.username=%E6%97%A0%E5%8A%9B

跟原包差不多

Orders

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
* 订单POJO
*/
public class Orders {
private Integer ordersId;// 订单编号
private User user;// 用户

public Integer getOrdersId() {
return ordersId;
}

public void setOrdersId(Integer ordersId) {
this.ordersId = ordersId;
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

}

控制器,这里也是很正常的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

@Controller
public class OrdersController {

/*
* 向订单查询页面跳转
*/
@RequestMapping("/tofindOrdersWithUser")
public String tofindOrdersWithUser() {
return "orders";
}

@RequestMapping("findOrdersWithUser")
public String findOrdersWithUser(Orders orders) {
Integer ordersId = orders.getOrdersId();
User user = orders.getUser();
String username = user.getUsername();
System.out.println(ordersId);
System.out.println(username);
return "success";
}
}

jsp里不怎么一样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>订单查询</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/findOrdersWithUser" method="post">
订单编号:<input type="text" name="ordersId"/><br/>
所属用户:<input type="text" name="user.username"/><br/>
<input type="submit" value="查询"/>
</form>
</body>
</html>

这里注意的是他是user.username

自定义数据绑定

日期格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class DateConverter implements Converter<String, Date> {

private String datePattern="yyyy-MM-dd HH:mm:ss";

@Override
public Date convert(String source) {
//格式化日期
SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
try {
return sdf.parse(source);
} catch (Exception e) {
throw new IllegalArgumentException("无效的日期格式");
}

}

}

xml配置

1
2
3
4
5
6
7
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.itheima.convert.DateConverter"></bean>
</set>
</property>
</bean>

DateController

1
2
3
4
5
6
7
8
9
10
11
@Controller
public class DateController {
/*
* 自定义类型绑定
*/
@RequestMapping("/customDate")
public String CustomDate(Date date) {
System.out.println("date=" + date);
return "success";
}
}

这样就可以,同时还可以用别的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* @author NoOne dotaerday@gmail.com:
* @version 创建时间:2020年3月10日 上午9:43:48 类说明
*/
public class DateFormatter implements Formatter<Date> {
private String datePattern = "yyyy-MM-dd HH:mm:ss";
private SimpleDateFormat simpleDatteformat;
@Override
public String print(Date date, Locale locale) {
return new SimpleDateFormat().format(date);
}

@Override
public Date parse(String source, Locale locale) throws ParseException {
simpleDatteformat = new SimpleDateFormat(datePattern);
return simpleDatteformat.parse(source);
}

}

xml

1
2
3
4
5
6
7
8
<!-- 自定义类型格式转换器 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
<set>
<bean class="com.itheima.convert.DateFormatter"></bean>
</set>
</property>
</bean>

复杂数据绑定

绑定数组

控制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
* 向用户列表页面跳转
*/
@RequestMapping("/toUser")
public String toUser() {
return "user";
}
/*
* 接受批量删除用户的方法
*/
@RequestMapping("/deleteUsers")
public String deleteUsers(Integer[] ids) {
if(ids != null) {
for (Integer id : ids) {
System.out.println("删除了id为" + id + "的用户");
}
}else {
System.out.println("ids不能为空");
}
return "success";
}

jsp

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
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户列表</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/deleteUsers"
method="post">
<table width="20%" border=1>
<tr>
<td>选择</td>
<td>用户名</td>
</tr>
<tr>
<td><input name="ids" value="1" type="checkbox"></td>
<td>tom</td>
</tr>
<tr>
<td><input name="ids" value="2" type="checkbox"></td>
<td>jack</td>
</tr>
<tr>
<td><input name="ids" value="3" type="checkbox"></td>
<td>lucy</td>
</tr>
</table>
<input type="submit" value="删除"/>
</form>
</body>
</html>

绑定集合

包装类里含集合

1
2
3
4
5
6
7
8
9
10
11
12
public class Uservo {
private List<User> users;

public List<User> getUsers() {
return users;
}

public void setUsers(List<User> users) {
this.users = users;
}

}

控制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@RequestMapping("/toUserEdit")
public String toUserEdit() {
return "user_edit";
}
/*
* 接受批量删除用户的方法
*/
@RequestMapping("/editUsers")
public String editUsers(Uservo userList) {
List<User> users = userList.getUsers();
for (User user : users) {
//如果接受的用户id不为空,则表示对该用户进行了修改
if(user.getId() != null) {
System.out.println("修改了id为" + user.getId() + "的用户名为" + user.getUsername());
}
}
return "success";
}

jsp

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
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户列表</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/editUsers"
method="post">
<table width="30%" border=1>
<tr>
<td>选择</td>
<td>用户名</td>
</tr>
<tr>
<td><input name="users[0].id" value="1" type="checkbox"></td>
<td><input name="users[0].username" value="tom" type="text"></td>
</tr>
<tr>
<td><input name="users[1].id" value="2" type="checkbox"></td>
<td><input name="users[1].username" value="jack" type="text"></td>
</tr>
</table>
<input type="submit" value="修改"/>
</form>
</body>
</html>

jsp里要注意是users[0].username 这种

本文作者:NoOne
本文地址https://noonegroup.xyz/posts/e83ba87b/
版权声明:转载请注明出处!