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

  由于日期数据有很多种格式,所以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中日期格式的转换的更多相关文章

  1. SpringMVC中日期格式的转换

    解决日期提交转换异常的问题 由于日期数据有很多种格式,所以springmvc没办法把字符串转换成日期类型.所以需要自定义参数绑定.前端控制器接收到请求后,找到注解形式的处理器适配器,对RequestM ...

  2. java中日期格式的转换和应用

    java中主要有3个类用于日期格式转换    DateFormat .SimpleDateFormat.Calendar SimpleDateFormat函数的继承关系: java.lang.Obje ...

  3. JS和vue中日期格式的转换

    1.获取当前时间: var now=new Date(); //Tue Oct 17 2017 18:08:40 GMT+0800 (中国标准时间) 获取当前时间的日期 new Date().getD ...

  4. springMVC 前后台日期格式传值解决方式之二(共二) @InitBinder的使用

    关于springmvc日期问题的解决方式 除了本博客的[springMVC 前后台日期格式传值解决方式之 @DateTimeFormat的使用和配置]一文, 还有如下这种方式: 在Controller ...

  5. php中时间戳和日期格式的转换

    一,PHP时间戳函数获取指定日期的unix时间戳 strtotime(”2009-1-22″) 示例如下: echo strtotime(”2009-1-22″) 结果:1232553600 说明:返 ...

  6. SpringMVC对日期类型的转换

    在做web开发的时候,页面传入的都是String类型,SpringMVC可以对一些基本的类型进行转换,但是对于日期类的转换可能就需要我们配置. 1.如果查询类使我们自己写,那么在属性前面加上@Date ...

  7. SpringMVC对日期类型的转换@ResponseBody返回的DateTime是long类型

    目前,多数web开发这都在使用Spring的框架.但是这个框架有个 @ResponseBody 注解返回json时,日期格式默认显示为时间戳. 而我们页面展示的时候一般都是以下格式: yyyy-MM- ...

  8. springmvc处理日期格式

    解决http400错误 通常有两个来源: 1 页面的下拉列表中传入了字符串,而服务器需要的是Integer类型的,所以服务器拒绝. 2, 浏览器传给服务器端的日期格式字符串,服务器端理解不了,所以需要 ...

  9. oracle中时间格式的转换

    1:取得当前日期是本月的第几周  select to_char(sysdate,'YYYYMMDD W HH24:MI:SS') from dual; TO_CHAR(SYSDATE,'YY') se ...

随机推荐

  1. Matplotlib.pyplot 常用方法

    1.介绍 Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形.通过 Matplotlib,开发者可以仅需要几行代码,便可以生成绘图 ...

  2. POJ 3525 Most Distant Point from the Sea 二分+半平面交

    题目就是求多变形内部一点. 使得到任意边距离中的最小值最大. 那么我们想一下,可以发现其实求是看一个圆是否能放进这个多边形中. 那么我们就二分这个半径r,然后将多边形的每条边都往内退r距离. 求半平面 ...

  3. JPA(七):映射关联关系------映射双向多对一的关联关系

    映射双向多对一的关联关系 修改Customer.java package com.dx.jpa.singlemanytoone; import java.util.Date; import java. ...

  4. Mac下brew/memcached/nginx/iterm/zsh的安装

    brew  https://www.cnblogs.com/fireworld/p/8609190.html memcached https://blog.csdn.net/whereismatrix ...

  5. Modbus常用功能码协议详解

    Modbus常用功能码协议详解 01H-读线圈状态 1)描述:读从机线圈寄存器,位操作,可读单个或者多个: 2)发送指令: 假设从机地址位0x01,寄存器开始地址0x0023,寄存器结束抵制0x003 ...

  6. Cocos2d-X研究之v3.x 事件分发机制具体解释

    事件分发机制 " src="http://www.cgzhw.com/wp-content/uploads/2014/07/inherent3.png" style=&q ...

  7. IntelliJ - idea15.0.2 破解方法

    由于idea 15版本更换了注册方式,只能通过联网激活,所以现在不能通过简单的通用注册码进行离线注册了, 虽然可以继续用14版本,但是有新版本却无法尝试让强迫症也是异常抓狂. 通过度娘我找到了一个破解 ...

  8. NameNode的ZKFC机制

    转自: http://hackershell.cn/?p=821 NameNode的HA可以个人认为简单分为共享editLog机制和ZKFC对NameNode状态的控制 在此之前,我先提几个问题: 一 ...

  9. ExtJs4.2中Tab选项卡的右击关闭其它和关闭当前功能不准确的解决方法

    一.ExtJs4.2中Tab选项卡的右击关闭其它和关闭当前功能不准确的解决方法 二.找到ux目录下的TabCloseMenu.js文件,将内容替换成下面代码. 三.代码: /** * Plugin f ...

  10. Nginx源代码分析—业务流程

    Nginx源代码分析-业务流程 到此为止,我们如果ngx_init_cycle已经结束.我们临时无论他做了什么,我们从他做的效果进入. 从常理上来讲,假设一个请求到达,那么我们须要接受这个请求,那么就 ...