目录:
- 应用场景
- 实现方法
[一]、应用场景
在实际应用中,经常会碰到表单中的日期 字符串和Javabean中的日期类型的属性自动转换,一般页面输入的日志格式为:yyyy-MM-dd ,而SpringMVC中默认不支持这样的格式转换,所以需要我们自定义数据类型的绑定才能实现这个功能。
[二]、实现方法
利用 WebBindingInitializer 注册自定义日期转换控制器。
自定义日期转换器:MyDataBinding.java
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
|
packagecom.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 <a href="http://www.micmiu.com">Michael Sun</a>
*/
publicclassMyDataBindingimplementsWebBindingInitializer{
publicvoidinitBinder(WebDataBinder binder,WebRequest request){
SimpleDateFormat dateFormat=newSimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
SimpleDateFormat datetimeFormat=newSimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
datetimeFormat.setLenient(false);
binder.registerCustomEditor(java.util.Date.class,newCustomDateEditor(
dateFormat,true));
binder.registerCustomEditor(java.sql.Timestamp.class,
newCustomTimestampEditor(datetimeFormat,true));
}
}
|
Timestamp 的实现:CustomTimestampEditor.java
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
packagecom.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 <a href="http://www.micmiu.com">Michael Sun</a>
*/
publicclassCustomTimestampEditorextendsPropertyEditorSupport{
privatefinalSimpleDateFormat dateFormat;
privatefinalbooleanallowEmpty;
privatefinalintexactDateLength;
publicCustomTimestampEditor(SimpleDateFormat dateFormat,booleanallowEmpty){
this.dateFormat=dateFormat;
this.allowEmpty=allowEmpty;
this.exactDateLength=-1;
}
publicCustomTimestampEditor(SimpleDateFormat dateFormat,
booleanallowEmpty,intexactDateLength){
this.dateFormat=dateFormat;
this.allowEmpty=allowEmpty;
this.exactDateLength=exactDateLength;
}
publicvoidsetAsText(Stringtext)throwsIllegalArgumentException{
if((this.allowEmpty)&&(!(StringUtils.hasText(text)))){
setValue(null);
}else{
if((text!=null)&&(this.exactDateLength>=0)
&&(text.length()!=this.exactDateLength)){
thrownewIllegalArgumentException(
"Could not parse date: it is not exactly"
+this.exactDateLength+"characters long");
}
try{
setValue(newTimestamp(this.dateFormat.parse(text).getTime()));
}catch(ParseException ex){
thrownewIllegalArgumentException("Could not parse date: "
+ex.getMessage(),ex);
}
}
}
publicStringgetAsText(){
Timestamp value=(Timestamp)getValue();
return((value!=null)?this.dateFormat.format(value):"");
}
}
|
修改spring-mvc 的配置文件,添加 webBindingInitializer 属性的注入配置:
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
|
<!--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>
|
这样就可以实现表单中的字符串自动转换为Date或者Timestamp 类型。
相关推荐
SpringMVC自定义多视图,多视图实现
自定义注解:SpringBoot中针对SpringMvc自定义注解获取用户登陆信息
SpringMVC表单标签进行数据绑定(图书管理)DemoSpringMVC表单标签进行数据绑定(图书管理)Demo
springmvc自定义属性编辑器和参数解析器
SpringMVC数据绑定及数据类型转换 文档中付例子
主要介绍了SpringMVC自定义类型转换器实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
springMVC自定义防重复提交,通过标签的方式实现
主要介绍了SpringMvc自定义拦截器(注解)代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Springmvc前后台数据数组绑定,list超过了256时,会报错,如何解决?该文给出了说明
SSM笔记-SpringMVC自定义视图及重定向视图、语言国际化
SpringMVC入门很简单之数据绑定(下),具体参考博文:http://www.cnblogs.com/liukemng/tag/SpringMVC/
主要介绍了SpringMVC自定义参数绑定实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了Springmvc自定义类型转换器实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了SpringMVC自定义拦截器实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
spring MVC数据绑定 含例子 转载自疯芒毕露的专栏 刚开始用spring mvc 做web开发时 经常会不知道如何合适绑定页面数据 用惯struts2的朋友更认为spring mvc 绑定数据不如struts2方便 本人最开始也是这么认为 经过一段...
主要介绍了Spring MVC自定义日期类型转换器实例详解的相关资料,需要的朋友可以参考下
Spring+SpringMvc+MybatisPlus实现多数据源切换、利用自定义Aop注解,只需要在需要切换数据库的方法上加上注解即可实现、极大避免了代码冗余。
springmvc数据绑定。
SpringMVC入门很简单之数据绑定(上),具体参考博文:http://www.cnblogs.com/liukemng/tag/SpringMVC/
本文抛砖引玉,并没有详细的介绍更全面的内容,通过一个例子让初次使用的人能够快速入门,简单的介绍一下。 第一,注解: @Before – 目标