关于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. VLC for iOS 2.3.0

    http://www.cocoachina.com/bbs/read.php?tid=231898 VLC for iOS 2.3.0       本帖属于CocoaChina会员发表,转帖请写明来源 ...

  2. MFC中CTime获取日期时间的方法

    MFC中CTime类的功能非常强大,可以获取年.月.日.小时.分钟.秒.星期等等,最最重要的是可根据需要去格式化.下面是具体的使用方式: ① 定义一个CTime类对象 CTime time; ② 得到 ...

  3. 配置wpa_supplicant调试wifi linux下命令行连接wifi

    调试wpa_supplicant先读一下对应的README.txt文件.根据README.txt修改编译配置. wpa_supplicant需要打开以下开关进行编译CONFIG_DRIVER_NL80 ...

  4. c/c++类型转换相关总结

    在c语言中存在两种类型转换:显式类型转换和隐式类型转换: 显示类型转换:在类型前加上(type)变量,对变量进行的转换,程序员自己显式添加: char *ptra = (char*)ptrb; voi ...

  5. nvm: node版本管理工具

    安装nvm   curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | bash node 版本切 ...

  6. 【linux高级程序设计】(第十三章)Linux Socket网络编程基础 2

    BSD Socket网络编程API 创建socket对象 int socket (int __domain, int __type, int __protocol) :成功返回socket文件描述符, ...

  7. hiho一下第128周 后缀自动机二·重复旋律5

    #1445 : 后缀自动机二·重复旋律5 时间限制:10000ms 单点时限:2000ms 内存限制:512MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为一段数构成的数 ...

  8. 【BZOJ3524】Couriers

    题面 Description 给一个长度为\(n\)的序列\(a\).\(1\le a[i]\le n\). \(m\)组询问,每次询问一个区间\([l,r]\),是否存在一个数在\([l,r]\)中 ...

  9. iptables 要点总结

    http://jiayu0x.com/2014/12/02/iptables-essential-summary/

  10. Android简单的利用MediaRecorder进行录音的实例代码

    MainActivity.java package com.example.soundrecord_demo; import java.io.IOException; import android.m ...