关于springmvc日期问题的解决方式 除了本博客的【springMVC 前后台日期格式传值解决方式之 @DateTimeFormat的使用和配置】一文,

还有如下这种方式:

在Controller里加上这段代码:

     @InitBinder
public void initBinder(ServletRequestDataBinder binder) {
/**
* 自动转换日期类型的字段格式
*/
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true)); }

注意,如果前台有多重日期格式,写成类似下面的方式是没有什么卵用的

     @InitBinder
public void initBinder(ServletRequestDataBinder binder) {
/**
* 自动转换日期类型的字段格式
*/
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月");
try {
binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf2, true));
}catch(Exception e) {
binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf1, true));
} }

怎么解决呢?

可参考这个问题:http://bbs.csdn.net/topics/380055180

原文内容如下

----------------------------------------------------------分界线开始-----------------------------------------------------------

配置文件为

1
2
3
4
5
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
    <property name="webBindingInitializer">  
        <bean class="xx.xxx.MyBindingInitializer"/>  
    </property>  
</bean

MyBindingInitializer中,initBinder方法里的
binder.registerCustomEditor(Date.class, new XXXEditor());

在XXXEditor的setAsText方法中,使用系统所有可能用到的format格式一一尝试,捕获异常,最后正确绑定。

-------------------------------------------------------分界线结束----------------------------------------------------------------

至于其中的XXXEditor怎么写,大家可以参照例子中的CustomDateEditor,即:org.springframework.beans.propertyeditors.CustomDateEditor源码中怎么写的。

springMVC 前后台日期格式传值解决方式之二(共二) @InitBinder的使用的更多相关文章

  1. springMVC 前后台日期格式传值解决方式之一(共二) @DateTimeFormat的使用和配置

    无意中发现对于时间字符串转Date类,根本不用自己去写转换类,spring mvc已经实现了该功能,还是基于注解的,轻松省事,使用 org.springframework.format.support ...

  2. springmvc处理日期格式

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

  3. SpringMVC中日期格式的转换

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

  4. 转:SpringMVC中日期格式的转换

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

  5. SpringMVC post和get乱码解决方式

    如何解决POST请求中文乱码问题,GET的又如何处理呢? 在web.xml中加入: <filter>     <filter-name>CharacterEncodingFil ...

  6. SpringMVC问题- MultipartConfig 配置问题以及解决方式

    http://www.cnblogs.com/weilu2/p/springmvc_fileupload_with_servlet_3_0.html

  7. 【转载】SpringMVC前台给后台传值的方式

    转自:http://blog.csdn.net/flymoringbird/article/details/53126505 1. 基本数据类型(以int为例,其他类似): Controller代码: ...

  8. NPOI EXECL数据导入,日期格式调用DateCellValue取值时,二次或后续调用出现报错!

    NPOI version:2.5.1 EXCEL数据导入功能,第一次调用DateCellValue获得日期值OK,二次或后续调用出现报错"函数求值需要运行所有线程" 初步怀疑是版本 ...

  9. 关于Java中文乱码与日期格式

    关于Java中文乱码与日期格式 Java中文乱码的问题其实很普遍了,首先,一般不会在Windows平台下面出现,同时,一般在Tomcat应用服务器下也都正常,但是到了WebSphere,Weblogi ...

随机推荐

  1. Selenium2+python自动化5-操作浏览器基本方法【转载】

    前言前面已经把环境搭建好了,这从这篇开始,正式学习selenium的webdriver框架.我们平常说的 selenium自动化,其实它并不是类似于QTP之类的有GUI界面的可视化工具,我们要学的是w ...

  2. POJ 2226.Muddy Fields-二分图最大匹配(最小点覆盖)

    Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12565   Accepted: 4651 Des ...

  3. ASP.NET MVC4 MVC 当前上下文中不存在名称“Scripts”

    Views目录下的web.config文件 <pages>下<namespaces>下 加入<add namespace="System.Web.Optimiz ...

  4. Logger Rate Limiter -- LeetCode

    Design a logger system that receive stream of messages along with its timestamps, each message shoul ...

  5. POJ 3713 Transferring Sylla (三连通图)

    [题目链接] http://poj.org/problem?id=3713 [题目大意] 给出一个图判断是不是三连通图,三连通图的意思是对于图中任意两点, 至少有三条路是可以相互连通的. [题解] 我 ...

  6. [COCI2015]ZGODAN

    题目大意: 给你一个数$n(n\leq10^1000)$,定义一个数是“美丽数”当且仅当这个数各个数位上的数奇偶性不同. 求最接近$n$的“美丽数”,若有多个,则依次输出. 思路: 贪心+高精度. 首 ...

  7. [JZOJ3106]锻炼

    题目大意: 给你一个$n\times m(n,m\leq 50)$的网格图,其中有一个四连通的障碍物.给定起点和终点,每次你可以走到和当前位置八连通的一个方格内,问绕障碍物一圈最短要走几格? 思路: ...

  8. Java多线程设计模式(2)生产者与消费者模式

    1 Producer-Consumer Pattern Producer-Consumer Pattern主要就是在生产者与消费者之间建立一个“桥梁参与者”,用来解决生产者线程与消费者线程之间速度的不 ...

  9. APPENDIX: How to apply the Apache License to your work

    To apply the Apache License to your work, attach the following boilerplate notice, with the fields e ...

  10. 【微信】1.微信小程序开发--入门

    开始开发微信小程序咯!! ============================= 1.找到官网API地址 https://developers.weixin.qq.com/miniprogram/ ...