转:SpringMVC中日期格式的转换

  解决日期提交转换异常的问题

  由于日期数据有很多种格式,所以springmvc没办法把字符串转换成日期类型。所以需要自定义参数绑定。前端控制器接收到请求后,找到注解形式的处理器适配器,对RequestMapping标记的方法进行适配,并对方法中的形参进行参数绑定。在springmvc这可以在处理器适配器上自定义Converter进行参数绑定。如果使用<mvc:annotation-driven/>可以在此标签上进行扩展。

  1.自定义DataConvertor类, 并实现Convertor接口

 1 public class DateConverter implements Converter<String, Date> {
 2 
 3      @Override
 4 
 5      public Date convert(String source) {
 6 
 7            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 8 
 9            try {
10 
11                 return simpleDateFormat.parse(source);
12 
13            } catch (ParseException e) {
14 
15                 e.printStackTrace();
16 
17            }
18 
19            return null;
20 
21      }
22 
23 }

  2.在springmvc.xml配置文件中注册转换器

  1 方法一:通过注解驱动的方式加载转换器
  2 
  3 <!-- 配置mvc注解驱动 -->
  4     <mvc:annotation-driven conversion-service="conversionService"/>
  5     
  6     <!-- 配置日期转换器 -->
  7     <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
  8         <property name="converters">
  9             <set>
 10                 <bean class="cn.rodge.ssm.converter.DateConverter"></bean>
 11             </set>
 12         </property>
 13     </bean>
 14 
 15   方法二:通过自定义webBinder配置(不常用)
 16 
 17 <?xml version="1.0" encoding="UTF-8"?>
 18 
 19 <beans xmlns="http://www.springframework.org/schema/beans"
 20 
 21      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 22 
 23      xmlns:context="http://www.springframework.org/schema/context"
 24 
 25      xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
 26 
 27      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 28 
 29         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
 30 
 31         http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
 32 
 33         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 34 
 35  
 36 
 37      <!-- 扫描带Controller注解的类 -->
 38 
 39      <context:component-scan base-package="cn.itcast.springmvc.controller" />
 40 
 41     
 42 
 43      <!-- 转换器配置 -->
 44 
 45      <bean id="conversionService"
 46 
 47         class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
 48 
 49            <property name="converters">
 50 
 51                 <set>
 52 
 53                      <bean class="cn.itcast.springmvc.convert.DateConverter"/>
 54 
 55                 </set>
 56 
 57            </property>
 58 
 59      </bean>
 60 
 61      <!-- 自定义webBinder -->
 62 
 63      <bean id="customBinder"     class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
 64 
 65            <property name="conversionService" ref="conversionService" />
 66 
 67      </bean>
 68 
 69      <!--注解适配器 -->
 70 
 71      <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
 72 
 73             <property name="webBindingInitializer" ref="customBinder"></property>
 74 
 75      </bean>
 76 
 77      <!-- 注解处理器映射器 -->
 78 
 79      <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
 80 
 81      <!-- 加载注解驱动 -->
 82 
 83      <!-- <mvc:annotation-driven/> -->
 84 
 85      <!-- 视图解析器 -->
 86 
 87      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 88 
 89            <property name="viewClass"
 90 
 91                 value="org.springframework.web.servlet.view.JstlView" />
 92 
 93            <!-- jsp前缀 -->
 94 
 95            <property name="prefix" value="/WEB-INF/jsp/" />
 96 
 97            <!-- jsp后缀 -->
 98 
 99            <property name="suffix" value=".jsp" />
100 
101      </bean>
102 
103 </beans>

  注意:此方法需要独立配置处理器映射器、适配器,不再使用<mvc:annotation-driven/>

posted @ 2017-03-16 10:06  ngulc  阅读(3039)  评论(0编辑  收藏  举报