登录  /  注册
首页 > Java > java教程 > 正文
SpringMVC中日期格式转换的示例代码详解
黄舟
发布: 2017-03-16 09:59:17
原创
1273人浏览过

本文主要介绍了SpringMVC中日期格式转换的相关知识:用来解决日期提交转换异常的问题。具有很好的参考价值。下面跟着小编一起来看下吧

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

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

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


public class DateConverter implements Converter<String, Date> {
   @Override
   public Date convert(String source) {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      try {
        return simpleDateFormat.parse(source);
      } catch (ParseException e) {
        e.printStackTrace();
      }
      return null;
   }
}
登录后复制

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

方法一:通过注解驱动的方式加载转换器


<!-- 配置mvc注解驱动 -->
  <mvc:annotation-driven conversion-service="conversionService"/>
  <!-- 配置日期转换器 -->
  <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="converters">
      <set>
        <bean class="cn.rodge.ssm.converter.DateConverter"></bean>
      </set>
    </property>
  </bean>
登录后复制

方法二:通过自定义webBinder配置(不常用)


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
   <!-- 扫描带Controller注解的类 -->
   <context:component-scan base-package="cn.itcast.springmvc.controller" />
   <!-- 转换器配置 -->
   <bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
      <property name="converters">
        <set>
           <bean class="cn.itcast.springmvc.convert.DateConverter"/>
        </set>
      </property>
   </bean>
   <!-- 自定义webBinder -->
   <bean id="customBinder"   class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
      <property name="conversionService" ref="conversionService" />
   </bean>
   <!--注解适配器 -->
   <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
      <property name="webBindingInitializer" ref="customBinder"></property>
   </bean>
   <!-- 注解处理器映射器 -->
   <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
   <!-- 加载注解驱动 -->
   <!-- <mvc:annotation-driven/> -->
   <!-- 视图解析器 -->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
      <!-- jsp前缀 -->
      <property name="prefix" value="/WEB-INF/jsp/" />
      <!-- jsp后缀 -->
      <property name="suffix" value=".jsp" />
   </bean>
</beans>
登录后复制

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

以上就是SpringMVC中日期格式转换的示例代码详解的详细内容,更多请关注php中文网其它相关文章!

来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学