目录:
- 应用场景
- 实现方法
[一]、应用场景
在实际应用中,经常会碰到表单中的日期 字符串和Javabean中的日期类型的属性自动转换,一般页面输入的日志格式为:yyyy-MM-dd ,而SpringMVC中默认不支持这样的格式转换,所以需要我们自定义数据类型的绑定才能实现这个功能。
[二]、实现方法
利用 WebBindingInitializer 注册自定义日期转换控制器。
自定义日期转换器:MyDataBinding.java
package com.micmiu.demo.web.v1.utils; import java.text.SimpleDateFormat; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.support.WebBindingInitializer; import org.springframework.web.context.request.WebRequest; /** * 自定义日期、时间的类型绑定 * * @author */ public class MyDataBinding implements WebBindingInitializer { public void initBinder(WebDataBinder binder, WebRequest request) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); SimpleDateFormat datetimeFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); datetimeFormat.setLenient(false); binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor( dateFormat, true)); binder.registerCustomEditor(java.sql.Timestamp.class, new CustomTimestampEditor(datetimeFormat, true)); } }1
Timestamp 的实现:CustomTimestampEditor.java
package com.micmiu.demo.web.v1.utils; import java.beans.PropertyEditorSupport; import java.sql.Timestamp; import java.text.SimpleDateFormat; import org.springframework.util.StringUtils; import java.text.ParseException; /** * Property editor for <code>java.sql.Timestamp</code>,<br> * supporting a custom <code>java.text.DateFormat</code>. * * @author */ public class CustomTimestampEditor extends PropertyEditorSupport { private final SimpleDateFormat dateFormat; private final boolean allowEmpty; private final int exactDateLength; public CustomTimestampEditor(SimpleDateFormat dateFormat, boolean allowEmpty) { this.dateFormat = dateFormat; this.allowEmpty = allowEmpty; this.exactDateLength = -1; } public CustomTimestampEditor(SimpleDateFormat dateFormat, boolean allowEmpty, int exactDateLength) { this.dateFormat = dateFormat; this.allowEmpty = allowEmpty; this.exactDateLength = exactDateLength; } public void setAsText(String text) throws IllegalArgumentException { if ((this.allowEmpty) && (!(StringUtils.hasText(text)))) { setValue(null); } else { if ((text != null) && (this.exactDateLength >= 0) && (text.length() != this.exactDateLength)) { throw new IllegalArgumentException( "Could not parse date: it is not exactly" + this.exactDateLength + "characters long"); } try { setValue(new Timestamp(this.dateFormat.parse(text).getTime())); } catch (ParseException ex) { throw new IllegalArgumentException("Could not parse date: " + ex.getMessage(), ex); } } } public String getAsText() { Timestamp value = (Timestamp) getValue(); return ((value != null) ? this.dateFormat.format(value) : ""); } }
修改spring-mvc 的配置文件,添加 webBindingInitializer 属性的注入配置:
<!--Spring3.1 之后的自定义注解 HandlerAdapter --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="webBindingInitializer"> <bean class="com.micmiu.demo.web.v1.utils.MyDataBinding" /> </property> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="writeAcceptCharset" value="false" /> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <value>*/*;charset=UTF-8</value> </list> </property> </bean> <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" /> <bean class="org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter" /> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean> </list> </property> </bean>
或者你可以这样使用
使用了spring3.2提供的全局@ControllerAdvice,代码如下: @ControllerAdvice public class GlobalController { @InitBinder public void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); datetimeFormat.setLenient(false); //自动转换日期类型的字段格式 binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); binder.registerCustomEditor(java.sql.Timestamp.class, new CustomTimestampEditor(datetimeFormat, true)); //防止XSS攻击 binder.registerCustomEditor(String.class, new StringEscapeEditor(true, false)); } }此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/70076.html