目录:
- 应用场景
- 实现方法
[一]、应用场景
在实际应用中,经常会碰到表单中的日期 字符串和Javabean中的日期类型的属性自动转换,一般页面输入的日志格式为:yyyy-MM-dd ,而SpringMVC中默认不支持这样的格式转换,所以需要我们自定义数据类型的绑定才能实现这个功能。
[二]、实现方法
利用 WebBindingInitializer 注册自定义日期转换控制器。
自定义日期转换器:MyDataBinding.java
1 | package com.micmiu.demo.web.v1.utils; |
3 | import java.text.SimpleDateFormat; |
5 | import org.springframework.beans.propertyeditors.CustomDateEditor; |
6 | import org.springframework.web.bind.WebDataBinder; |
7 | import org.springframework.web.bind.support.WebBindingInitializer; |
8 | import org.springframework.web.context.request.WebRequest; |
15 | public class MyDataBinding implements WebBindingInitializer { |
17 | public void initBinder(WebDataBinder binder, WebRequest request) { |
18 | SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" ); |
19 | dateFormat.setLenient( false ); |
21 | SimpleDateFormat datetimeFormat = new SimpleDateFormat( |
22 | "yyyy-MM-dd HH:mm:ss" ); |
23 | datetimeFormat.setLenient( false ); |
25 | binder.registerCustomEditor(java.util.Date. class , new CustomDateEditor( |
27 | binder.registerCustomEditor(java.sql.Timestamp. class , |
28 | new CustomTimestampEditor(datetimeFormat, true )); |
Timestamp 的实现:CustomTimestampEditor.java
1 | package com.micmiu.demo.web.v1.utils; |
3 | import java.beans.PropertyEditorSupport; |
4 | import java.sql.Timestamp; |
5 | import java.text.SimpleDateFormat; |
6 | import org.springframework.util.StringUtils; |
7 | import java.text.ParseException; |
10 | * Property editor for <code>java.sql.Timestamp</code>,<br> |
11 | * supporting a custom <code>java.text.DateFormat</code>. |
15 | public class CustomTimestampEditor extends PropertyEditorSupport { |
17 | private final SimpleDateFormat dateFormat; |
18 | private final boolean allowEmpty; |
19 | private final int exactDateLength; |
21 | public CustomTimestampEditor(SimpleDateFormat dateFormat, boolean allowEmpty) { |
22 | this .dateFormat = dateFormat; |
23 | this .allowEmpty = allowEmpty; |
24 | this .exactDateLength = - 1 ; |
27 | public CustomTimestampEditor(SimpleDateFormat dateFormat, |
28 | boolean allowEmpty, int exactDateLength) { |
29 | this .dateFormat = dateFormat; |
30 | this .allowEmpty = allowEmpty; |
31 | this .exactDateLength = exactDateLength; |
34 | public void setAsText(String text) throws IllegalArgumentException { |
35 | if (( this .allowEmpty) && (!(StringUtils.hasText(text)))) { |
38 | if ((text != null ) && ( this .exactDateLength >= 0 ) |
39 | && (text.length() != this .exactDateLength)) { |
40 | throw new IllegalArgumentException( |
41 | "Could not parse date: it is not exactly" |
42 | + this .exactDateLength + "characters long" ); |
45 | setValue( new Timestamp( this .dateFormat.parse(text).getTime())); |
46 | } catch (ParseException ex) { |
47 | throw new IllegalArgumentException( "Could not parse date: " |
48 | + ex.getMessage(), ex); |
53 | public String getAsText() { |
54 | Timestamp value = (Timestamp) getValue(); |
55 | return ((value != null ) ? this .dateFormat.format(value) : "" ); |
修改spring-mvc 的配置文件,添加 webBindingInitializer 属性的注入配置:
3 | class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" > |
4 | < property name = "webBindingInitializer" > |
5 | < bean class = "com.micmiu.demo.web.v1.utils.MyDataBinding" /> |
7 | < property name = "messageConverters" > |
10 | class = "org.springframework.http.converter.ByteArrayHttpMessageConverter" /> |
12 | class = "org.springframework.http.converter.StringHttpMessageConverter" > |
13 | < property name = "writeAcceptCharset" value = "false" /> |
14 | < property name = "supportedMediaTypes" > |
16 | < value >text/plain;charset=UTF-8</ value > |
17 | < value >*/*;charset=UTF-8</ value > |
22 | class = "org.springframework.http.converter.xml.SourceHttpMessageConverter" /> |
24 | class = "org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter" /> |
26 | class = "org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" ></ bean > |
这样就可以实现表单中的字符串自动转换为Date或者Timestamp 类型。