SpringMVC
数据转换
-
springMVC
数据绑定流程
- 使用
ConversionService
转换数据
- 使用
@InitBinder
转换数据
- 使用
WebBindingInitializer
转换数据
1. springMVC
数据绑定流程
SpringMVC 数据绑定机制
2. 使用ConversionService
转换数据
2.1 自定义类型转换器
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.core.convert.converter.Converter;
public class StringToDateConverter implements Converter<String, Date> {
//日期类型模板。如:"yyyy-MM-dd"
private String datePattern;
public void setDatePattern(String datePattern) {
this.datePattern = datePattern;
}
// Converter<S,T>接口的类型转换方法
@Override
public Date convert(String date) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(this.datePattern);
// 将日期字符串转换成Date类型返回
return sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
System.out.println("日期转换失败!");
return null;
}
}
}
2.2 springMVC
配置文件中配置
<!-- 装配自定义的类型转换器 -->
<mvc:annotation-driven conversion-service="conversionService"/>
<!-- 自定义的类型转换器 -->
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.zhougl.web.converter.StringToDateConverter"
p:datePattern="yyyy-MM-dd"></bean>
</list>
</property>
</bean>
- 注必须引入声明
xmlns:p="http://www.springframework.org/schema/p"
,否则会报错:与元素类型 "bean" 相关联的属性 "p:datePattern" 的前缀 "p" 未绑定。
2.3 测试代码
2.3.1 实体类
ublic class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 8393614516445523837L;
private String loginName;
private Date birthday;
public User() {
super();
}
public User(String loginName,Date birthday) {
super();
this.loginName = loginName;
this.birthday = birthday;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
2.3.2 Controller控制层
@Controller
@RequestMapping(value="/converterTest")
public class UserController {
private static final Log logger = LogFactory.getLog(UserController.class);
@RequestMapping(value="/{formName}")
public String loginForm(@PathVariable String formName) {
//动态跳转页面
return "converterTest/"+formName;
}
@RequestMapping(value="/register",method=RequestMethod.POST)
public String register(@ModelAttribute User user,Model model) {
logger.info(user);
model.addAttribute("user", user);
return "converterTest/success";
}
}
2.3.3 jsp
/converterTest/registerForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试数据转换器</title>
</head>
<body>
<h3>注册页面</h3>
<form action="register" method="post">
<table>
<tr>
<td><label>登录名:</label></td>
<td><input type="text" id="loginName" name="loginName"></td>
</tr>
<tr>
<td><label>生日:</label></td>
<td><input type="text" id="birthday" name="birthday"></td>
</tr>
<tr>
<td><input type="submit" id="submit" value="登录"></td>
</tr>
</table>
</form>
</body>
</html>
/converterTest/success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试ConversionService</title>
</head>
<body>
登录名:${requestScope.user.loginName }<br>
生日:<fmt:formatDate value="${requestScope.user.birthday}"
pattern="yyyy年MM月dd日"/><br>
</body>
</html>
- 注:使用
jstl
标签需要引入jstl.jar
,否则会报错:org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jsp/jstl/fmt cannot be resolved in either web.xml or the jar files deployed with this application
网友评论