spring MVC中数据绑定之日期类型
2017-02-09 17:11
274 查看
spring MVC中数据绑定之日期类型
标签: springmvc数据绑定databind类型转换
2015-01-29 12:30 3164人阅读 评论(0) 收藏 举报
![](http://static.blog.csdn.net/images/category_icon.jpg)
分类:
J2EE(41)
![](http://static.blog.csdn.net/images/arrow_triangle%20_down.jpg)
版权声明:本文为博主原创文章,未经博主允许不得转载。
数据绑定应该算是spring MVC的特点之一吧~简单易用且功能强大,极大地简化了我们编程人员对于用户输入数据的接收及转换。
早先版本的Spring中的数据绑定完全都是基于PropertyEditor的。而Spring3中引入了Converter,用来替代PropertyEditor完成类型转换。
那么我们也依照这个顺序,先来讲讲基于传统的PropertyEditor来实现日期类型的数据绑定。
在Spring MVC中,我们可以通过WebDataBinder来注册自定义的PropertyEditor,从而添加对应的请求参数绑定。有两种方式:
1、使用@InitBinder注解@Controller中的方法 2、自定义WebBindingInitializer来提供一个全局的数据绑定规则。
1、使用@InitBinder注解
[java] view
plain copy
![](https://code.csdn.net/assets/CODE_ico.png)
@InitBinder
public void initBinder(WebDataBinder binder){
binder.registerCustomEditor(Date.class, new DateEditor());
}
[java] view
plain copy
![](https://code.csdn.net/assets/CODE_ico.png)
public class DateEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = format.parse(text);
} catch (ParseException e) {
format = new SimpleDateFormat("yyyy-MM-dd");
try {
date = format.parse(text);
} catch (ParseException e1) {
e1.printStackTrace();
}
}
setValue(date);
}
}
这里我们将DateEditor提出来封装成一个类方便重用。
另外这里有个try...catch的小妙用,就是首先以"yyyy-MM-dd HH:mm:ss"的形式来解析用户输入的参数,若解析失败则转以"yyyy-MM-dd"的形式来解析。这样的逻辑就可以同时处理"yyyy-MM-dd HH:mm:ss"和"yyyy-MM-dd"形式的日期数据,我想在一般的中文系统中,这两种形式应该是最常用的了吧。
添加如上代码之后,@InitBinder所在的Controller就可以自动绑定日期类型的数据了,不过这仅仅是在该Controller中生效,若想在全局范围内生效的话,可以将@InitBinder注解所在的Controller定义为一个BaseController,其余Controller都继承这个Controller。当然还有另外的方法,若你有兴趣的话,请看2。
2、自定义WebBindingInitializer
[java] view
plain copy
![](https://code.csdn.net/assets/CODE_ico.png)
public class MyWebBindingInitializer implements WebBindingInitializer {
@Override
public void initBinder(WebDataBinder binder, WebRequest request) {
binder.registerCustomEditor(Date.class, new DateEditor());
}
}
还是前面写的DateEditor,这么快又见面了,只不过注册的位置改变了,在WebBindingInitializer中注册的PropertyEditor是在全局范围内共享的。
不过光这样还不够,还要将WebBindingInitializer注入到AnnotationMethodHandlerAdapter中。
[html] view
plain copy
![](https://code.csdn.net/assets/CODE_ico.png)
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer">
<bean
class="org.springframework.samples.petclinic.web.ClinicBindingInitializer" />
</property>
</bean>
如果是用<mvc:annotation-driven />的童鞋,上面的配置就没效果了,而mvc命名空间也没提供如此细粒度的配置,怎么办呢?
别怕,方法还是有的,我们可以通过一个自定义PostBeanProcessor来处理:
[java] view
plain copy
![](https://code.csdn.net/assets/CODE_ico.png)
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
if (bean instanceof RequestMappingHandlerAdapter) {
RequestMappingHandlerAdapter adapter = (RequestMappingHandlerAdapter) bean;
adapter.setWebBindingInitializer(new MyWebBindingInitializer());
}
return bean;
}
不过实际上<mvc:annotation-driven />默认就为我们提供了一个WebBindingInitializer——ConfigurableWebBindingInitializer
而上面的方法则会覆盖默认的ConfigurableWebBindingInitializer,其实我们可以直接使用这个Bean来注册我们的PropertyEditor:
[java] view
plain copy
![](https://code.csdn.net/assets/CODE_ico.png)
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
if(bean instanceof ConfigurableWebBindingInitializer){
ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) bean;
initializer.setPropertyEditorRegistrar(new PropertyEditorRegistrar() {
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Date.class, new DateEditor());
}
});
}
return bean;
}
这里需要说明一下,WebBindingInitializer中不仅仅能注册PropertyEditor,还能注册Converter,也就是下面的3
3、使用ConverstionService
Spring3新引入了Converter系统,而ConversionService则是一个Facade类,用来封装底层实现而对外提供便捷的类型转换。所以这里不能重用之间的DateEditor了,不过大致逻辑还是一样的。另外补充说明一下,Converter是处理任意两类型间的转换,而Formatter是处理字符串和另一类型之间的转换的。可以看出来,Formatter是一类特殊的Converter,并且在处理数据绑定时,Formatter比Converter更加合适。所以我们这里就用Formatter来做:
[java] view
plain copy
![](https://code.csdn.net/assets/CODE_ico.png)
public class DateFormatter implements Formatter<Date> {
@Override
public String print(Date object, Locale locale) {
return null;
}
@Override
public Date parse(String text, Locale locale) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = format.parse(text);
} catch (Exception e) {
format = new SimpleDateFormat("yyyy-MM-dd");
date = format.parse(text);
}
return date;
}
}
这里我们只写String到Date的逻辑。然后需要将DateFormatter注册到一个ConversionService中,最后再将ConversionService注册到Spring MVC中。
[html] view
plain copy
![](https://code.csdn.net/assets/CODE_ico.png)
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
<set>
<bean class="com.test.common.core.DateFormatter"></bean>
</set>
</property>
</bean>
如果是用<mvc:annotation-driven />的童鞋,那么很简单,只需要:
[html] view
plain copy
![](https://code.csdn.net/assets/CODE_ico.png)
<mvc:annotation-driven conversion-service="conversionService"/>
而未使用<mvc:annotation-driven />的童鞋,需要定义一个WebBindingInitializer(或者使用ConfigurableWebBindingInitializer),然后注入到RequestMappingHandlerAdapter中去:
[html] view
plain copy
![](https://code.csdn.net/assets/CODE_ico.png)
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer" ref="webBindingInitializer">
</property>
</bean>
<bean id="webBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService" ref="conversionService"></property>
</bean>
此时可能有人会问,如果同时使用PropertyEditor和ConversionService,执行顺序是什么呢?内部首先查找PropertyEditor进行类型转换,如果没有找到相应的PropertyEditor再通过ConversionService进行转换。
4、PropertyEditor的自动注册
对了,这里再稍带提一下自动注册PropertyEditor,只需要将JavaBean和JavaBean名称+Editor这两个类放在同一包下,那么JavaBeans的基础结构会自动发现PropertyEditor的类,而无需你手动注册~
相关文章推荐
- Spring MVC中数据绑定之日期类型
- spring mvc表单数据绑定,对于基本类型和日期的处理WebDataBinder
- spring mvc表单数据绑定,对于基本类型和日期的处理WebDataBinder
- JavaEE_Mybatis_SpringMVC__Spring MVC中数据绑定之日期类型
- spring mvc表单复杂类型数据绑定
- 自定义日期类型的数据绑定 前台 - 后台 或 后台 - 前台 互相转换
- Spring MVC 解决日期类型动态绑定问题
- 自定义日期类型的数据绑定 前台 - 后台 或 后台 - 前台 互相转换
- SpringMVC自定义日期类型的数据绑定
- Spring MVC 自学杂记(二) -- 数据绑定之日期转换
- spring mvc 注解访问控制器以及接收form数据的方式,包括直接接收日期类型及对象的方法
- SpringMVC自定义日期类型的数据绑定
- 自定义日期类型的数据绑定 前台 - 后台 或 后台 - 前台 互相转换
- SpringMVC自定义日期类型的数据绑定
- spring mvc3(对象传参 Date 类型数据绑定)
- 自定义可绑定数据的业务对象实体和强类型-Part One
- 类库-----一些经常用到的对控件的处理1. 获取文本框中输入内容2.清除文本框内容3. 获取下拉列表框中选择内容,类型0为Text值,类型1为Value值4.DropDownList绑定日期
- 在Oracle中插入日期类型的数据
- 自定义可绑定数据的业务对象实体和强类型-Part Three
- 自定义可绑定数据的业务对象实体和强类型-Part Two