
正文
Spring MVC 之类型转换(五)
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
虽然SpringMVC可以自动绑定多种数据类型,但是有时候有些特殊的数据类型还是会在绑定时发生错误,需要我们自己书写类型转换完成绑定。
SpringMVC中提供两种绑定方式:
以时间转换为例
。
1、属性编辑器(传统方式)
控制器:
@RequestMapping(value="/login.do")
public String login(UserBean user){
log.info(user.getUsername());
log.info(user.getBirthday());
return "index";
}
// 自定义一个属性编辑器,用于转换时间类型
@InitBinder
public void converterStringDate(WebDataBinder binder){
binder.registerCustomEditor(Date.class, new DateEditor());
}
可以通过重写PropertyEditorSupport中的setAsText()定义自己的转换规则
package com.cy.springannotation.controller; import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; public class DateEditor extends PropertyEditorSupport { @Override
public void setAsText(String text) throws IllegalArgumentException {
Date date = null;
try {
if(text.contains("-")){
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
date = sf.parse(text);
}else {
SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy");
date = sf.parse(text);
}
} catch (ParseException e) {
e.printStackTrace();
}
this.setValue(date);
} }
2、类型转换器
Converter是Spring3提供的新的类型转换,相对于属性转换器更强大,可以把任意类型转换,而不是局限于String类型。
控制器:
@RequestMapping(value="/login.do")
public String login(UserBean user){
log.info(user.getUsername());
log.info(user.getBirthday());
return "index";
}
全局类型转换器
package com.cy.springannotation.controller; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; import org.springframework.core.convert.converter.Converter;
/**
* 全局类型转换器
* @author acer
*
*/
public class DateConvert implements Converter<String, Date>{
@Override
public Date convert(String text) {
Date date = null;
try {
if(text.contains("-")){
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
date = sf.parse(text);
}else {
SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy");
date = sf.parse(text);
} } catch (ParseException e) {
e.printStackTrace();
}
return date;
} }
类型转换器需要在配置文件中配置:
<!--开启注解 -->
<mvc:annotation-driven conversion-service="tc" /> <!--类型转换器工厂 --> <bean id="tc" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.cy.springannotation.controller.DateConvert" />
</list>
</property>
</bean>
共同的jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>登录页面</title>
<script type="text/javascript" src="<%=basePath%>js/jquery-2.1.4.js"></script>
<script type="text/javascript"> </script>
</head>
<body>
<form action="login.do" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>出生日期</td>
<td><input type="text" name="birthday"/></td>
</tr>
<tr>
<td colspan="2"> <input type="submit" value="提交"/> </td>
</tr>
</table>
</form>
</body>
</html>
结果显示:







