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

由于日期数据有很多种格式,所以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. C# Oracle数据库操作类实例详解

    本文所述为C#实现的Oracle数据库操作类,可执行超多常用的Oracle数据库操作,包含了基础数据库连接.关闭连接.输出记录集.执行Sql语句,返回带分页功能的dataset .取表里字段的类型和长 ...

  2. 升级CentOS5.6_X64 python2.4.3到2.7

    本文转自:http://hxl2009.blog.51cto.com/779549/1031310 升级CentOS 5.6 64位版python到2.7.31. 背景CentOS 5.6自带的Pyt ...

  3. 如何在Android上编写高效的Java代码

    转自:http://www.ituring.com.cn/article/177180 作者/ Erik Hellman Factor10咨询公司资深移动开发顾问,曾任索尼公司Android团队首席架 ...

  4. ZeroC Ice IceGrid Node和IceGrid

    IceGrid Node介绍 绝大多数分布式系统都有一个共同特点,即分布在各个主机上的节点进程并不是完全独立的,而是彼此之间有相互联系和通信的.集群对集群中的节点有一些控制指令,如部署.启停或者调整某 ...

  5. 【34】包含min函数的stack

    题目: 实现一个包含min函数的栈,min和push,pop都是o(1)时间 思路: 采用一个辅助的栈,来存储不同阶段的最小值 - 代码: push(int value){ //data是数据栈,mi ...

  6. 【Nginx】下载,请求限速,根据URL参数限速

    这个场景是限制单个连接的下载速度,还有限制单个IP的连接数,或者单位时间内的请求数,实验环境 nginx1.9.x. 小例子为主,具体的细节请多看文档. 限制下载速度 location /downlo ...

  7. ubuntu14.04下安装rubinius测试原生线程

    因为CRuby(MRI)本身不支持原生多线程,所以想试一下其他ruby解释器实现对原生多线程的支持.于是安装rubinius折腾一下:) 在rubinius官网下载2.4.1源代码,然后驾轻就熟首先b ...

  8. oracle的for和i++

    很长时间没用oracle的储存了,这次用到一次i++i++的sql语句:declarei_1 number(30) :=0;begin i_1 :=i_1+1;//i_1=1 insert into ...

  9. 服务端集成支付宝踩过的坑RSA、RSA2

    坑 在配置蚂蚁开发平台的时候,切记一定要注意选择的加密方式是RSA,还是RSA2.因为这两种方式生成的支付宝公匙是不一样的.RSA2对应的是2048位支付宝公匙.在配置类Config中,要根据加密方式 ...

  10. 春天的事务之9.3编程式事务 - 跟我学spring3

    9.3编程式事务 9.3.1编程式事务概述 所谓编程式事务指的是通过编码方式实现事务,即类似于JDBC编程实现事务管理. Spring框架提供一致的事务抽象,因此对于JDBC还是JTA事务都是采用相同 ...