SpringMVC自定义日期类型的数据绑定
2013-07-24 16:37
429 查看
目录:
应用场景
实现方法
[一]、应用场景
在实际应用中,经常会碰到表单中的日期字符串和Javabean中的日期类型的属性自动转换,一般页面输入的日志格式为:yyyy-MM-dd,而SpringMVC中默认不支持这样的格式转换,所以需要我们自定义数据类型的绑定才能实现这个功能。
[二]、实现方法
利用WebBindingInitializer注册自定义日期转换控制器。
自定义日期转换器:MyDataBinding.java
Timestamp的实现:CustomTimestampEditor.java
修改spring-mvc的配置文件,添加webBindingInitializer属性的注入配置:
这样就可以实现表单中的字符串自动转换为Date或者Timestamp类型。
本文介绍到此结束@MichaelSun.
原创文章,转载请注明:转载自micmiu
–大大的技术|小小的生活[http://www.micmiu.com/]
本文链接地址:http://www.micmiu.com/j2ee/spring/springmvc-binding-date/
应用场景
实现方法
[一]、应用场景
在实际应用中,经常会碰到表单中的日期字符串和Javabean中的日期类型的属性自动转换,一般页面输入的日志格式为:yyyy-MM-dd,而SpringMVC中默认不支持这样的格式转换,所以需要我们自定义数据类型的绑定才能实现这个功能。
[二]、实现方法
利用WebBindingInitializer注册自定义日期转换控制器。
自定义日期转换器:MyDataBinding.java
1 | package com.micmiu.demo.web.v1.utils; |
2 |
3 | import java.text.SimpleDateFormat; |
4 |
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; |
9 |
10 | /** |
11 | * 自定义日期、时间的类型绑定 |
12 | * |
13 | * |
14 | */ |
15 | public class MyDataBinding implements WebBindingInitializer { |
16 |
17 | public void initBinder(WebDataBinder |
18 | SimpleDateFormat new SimpleDateFormat( "yyyy-MM-dd" ); |
19 | dateFormat.setLenient( false ); |
20 |
21 | SimpleDateFormat new SimpleDateFormat( |
22 | "yyyy-MM-dd HH:mm:ss" ); |
23 | datetimeFormat.setLenient( false ); |
24 |
25 | binder.registerCustomEditor(java.util.Date. class , new CustomDateEditor( |
26 | dateFormat, true )); |
27 | binder.registerCustomEditor(java.sql.Timestamp. class , |
28 | new CustomTimestampEditor(datetimeFormat, true )); |
29 | } |
30 | } |
1 | package com.micmiu.demo.web.v1.utils; |
2 |
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; |
8 |
9 | /** |
10 | * |
11 | * |
12 | * |
13 | * |
14 | */ |
15 | public class CustomTimestampEditor extends PropertyEditorSupport { |
16 |
17 | private final SimpleDateFormat dateFormat; |
18 | private final boolean allowEmpty; |
19 | private final int exactDateLength; |
20 |
21 | public CustomTimestampEditor(SimpleDateFormat dateFormat, boolean allowEmpty) { |
22 | this .dateFormat |
23 | this .allowEmpty |
24 | this .exactDateLength 1 ; |
25 | } |
26 |
27 | public CustomTimestampEditor(SimpleDateFormat dateFormat, |
28 | boolean allowEmpty, int exactDateLength) { |
29 | this .dateFormat |
30 | this .allowEmpty |
31 | this .exactDateLength |
32 | } |
33 |
34 | public void setAsText(String throws IllegalArgumentException { |
35 | if (( this .allowEmpty) |
36 | setValue( null ); |
37 | } else { |
38 | if ((text null ) this .exactDateLength 0 ) |
39 | && this .exactDateLength)) { |
40 | throw new IllegalArgumentException( |
41 | "Could |
42 | + this .exactDateLength "characters long" ); |
43 | } |
44 | try { |
45 | setValue( new Timestamp( this .dateFormat.parse(text).getTime())); |
46 | } catch (ParseException |
47 | throw new IllegalArgumentException( "Could |
48 | + |
49 | } |
50 | } |
51 | } |
52 |
53 | public String |
54 | Timestamp |
55 | return ((value null ) this .dateFormat.format(value) "" ); |
56 | } |
57 | } |
1 | <!--Spring3.1 |
2 | < bean |
3 | class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" > |
4 | < property name = "webBindingInitializer" > |
5 | < bean class = "com.micmiu.demo.web.v1.utils.MyDataBinding" /> |
6 | </ property > |
7 | < property name = "messageConverters" > |
8 | < list > |
9 | < bean |
10 | class = "org.springframework.http.converter.ByteArrayHttpMessageConverter" /> |
11 | < bean |
12 | class = "org.springframework.http.converter.StringHttpMessageConverter" > |
13 | < property name = "writeAcceptCharset" value = "false" /> |
14 | < property name = "supportedMediaTypes" > |
15 | < list > |
16 | < value >text/plain;charset=UTF-8</ value > |
17 | < value >*/*;charset=UTF-8</ value > |
18 | </ list > |
19 | </ property > |
20 | </ bean > |
21 | < bean |
22 | class = "org.springframework.http.converter.xml.SourceHttpMessageConverter" /> |
23 | < bean |
24 | class = "org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter" /> |
25 | < bean |
26 | class = "org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" ></ bean > |
27 | </ list > |
28 | </ property > |
29 | </ bean > |
本文介绍到此结束@
原创文章,转载请注明:转载自micmiu
–大大的技术|小小的生活[
本文链接地址:
相关文章推荐
- SpringMVC自定义日期类型的数据绑定
- SpringMVC自定义日期类型的数据绑定
- JavaEE_Mybatis_SpringMVC_自定义的参数绑定,页面的日期类型传递到数据库中datetime
- 自定义日期类型的数据绑定 前台 - 后台 或 后台 - 前台 互相转换
- SpringMVC数据绑定二(自定义数据类型与复杂数据类型对象)
- 自定义日期类型的数据绑定 前台 - 后台 或 后台 - 前台 互相转换
- SpringMVC学习笔记(九)——简单类型、pojo类、自定义日期类型的参数绑定
- 自定义日期类型的数据绑定 前台 - 后台 或 后台 - 前台 互相转换
- SpringMVC 自定义参数绑定实现日期类型绑定
- JavaEE_Mybatis_SpringMVC__Spring MVC中数据绑定之日期类型
- SpringMVC类型转换、数据绑定详解
- 自定义可绑定数据的业务对象实体和强类型-Part Two
- springMVC中自定义json中数据类型
- springmvc自定义参数类型绑定
- SpringMVC类型转换、数据绑定
- springmvc自定义参数绑定实现日期参数绑定
- springmvc自定义数据绑定支持入参和出参snake和camel相互转化
- SpringMVC 使用HandlerMethodArgumentResolver自定义解析器实现请求数据绑定方法入参
- SpringMVC自定义日期格式进行参数绑定
- SpringMVC 自动绑定数据 - DATE多个类型格式 的数据绑定