SpringMVC中日期格式的转换

 更新时间:2017年03月15日 11:51:12   作者:rodge  
本文主要介绍了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/>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

相关文章

  • 聊聊Spring MVC JSON数据交互的问题

    聊聊Spring MVC JSON数据交互的问题

    我们在开发中后端经常需要接受来自于前端传递的Json字符串数据,怎么把Json字符串转换为Java对象呢?下面小编给大家带来了Spring MVC JSON数据交互的问题,感兴趣的朋友一起看看吧
    2021-10-10
  • springboot如何通过不同的策略动态调用不同的实现类

    springboot如何通过不同的策略动态调用不同的实现类

    这篇文章主要介绍了springboot如何通过不同的策略动态调用不同的实现类,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • 解决cmd运行java程序“找不到文件”提示的方案

    解决cmd运行java程序“找不到文件”提示的方案

    在本篇文章里小编给大家分享的是关于解决cmd运行java程序“找不到文件”提示的方案,有需要的朋友们可以参考下。
    2020-02-02
  • SpringBoot整合阿里云视频点播的过程详解

    SpringBoot整合阿里云视频点播的过程详解

    视频点播(ApsaraVideo for VoD)是集音视频采集、编辑、上传、自动化转码处理、媒体资源管理、分发加速于一体的一站式音视频点播解决方案。这篇文章主要介绍了SpringBoot整合阿里云视频点播的详细过程,需要的朋友可以参考下
    2021-12-12
  • Spring Security中successHandler无效问题及解决

    Spring Security中successHandler无效问题及解决

    这篇文章主要介绍了Spring Security中successHandler无效问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08
  • Java实战宠物店在线交易平台的实现流程

    Java实战宠物店在线交易平台的实现流程

    读万卷书不如行万里路,只学书上的理论是远远不够的,只有在实战中才能获得能力的提升,本篇文章手把手带你用java+Springboot+maven+Mysql+FreeMarker实现一个宠物在线交易系统,大家可以在过程中查缺补漏,提升水平
    2022-01-01
  • Java截取中英文混合字符串的方法

    Java截取中英文混合字符串的方法

    这篇文章主要为大家详细介绍了Java截取中英文混合字符串的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-06-06
  • Mybatisplus实现JSON处理器的示例代码

    Mybatisplus实现JSON处理器的示例代码

    Mybatisplusjson是基于Mybatisplus开发的一个json工具库,本文主要介绍了Mybatisplus实现JSON处理器的示例代码,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03
  • SpringBoot集成kafka全面实战记录

    SpringBoot集成kafka全面实战记录

    在实际开发中,我们可能有这样的需求,应用A从TopicA获取到消息,经过处理后转发到TopicB,再由应用B监听处理消息,即一个应用处理完成后将该消息转发至其他应用,完成消息的转发,这篇文章主要介绍了SpringBoot集成kafka全面实战,需要的朋友可以参考下
    2021-11-11
  • 浅谈对象与Map相互转化

    浅谈对象与Map相互转化

    这篇文章主要介绍了利用BeanMap进行对象与Map的相互转换,在文中列举了完整代码,需要的朋友可以参考下。
    2017-09-09

最新评论