springMVC 前后台日期格式传值解决方式之二(共二) @InitBinder的使用
关于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的使用的更多相关文章
- springMVC 前后台日期格式传值解决方式之一(共二) @DateTimeFormat的使用和配置
无意中发现对于时间字符串转Date类,根本不用自己去写转换类,spring mvc已经实现了该功能,还是基于注解的,轻松省事,使用 org.springframework.format.support ...
- springmvc处理日期格式
解决http400错误 通常有两个来源: 1 页面的下拉列表中传入了字符串,而服务器需要的是Integer类型的,所以服务器拒绝. 2, 浏览器传给服务器端的日期格式字符串,服务器端理解不了,所以需要 ...
- SpringMVC中日期格式的转换
解决日期提交转换异常的问题 由于日期数据有很多种格式,所以springmvc没办法把字符串转换成日期类型.所以需要自定义参数绑定.前端控制器接收到请求后,找到注解形式的处理器适配器,对RequestM ...
- 转:SpringMVC中日期格式的转换
解决日期提交转换异常的问题 由于日期数据有很多种格式,所以springmvc没办法把字符串转换成日期类型.所以需要自定义参数绑定.前端控制器接收到请求后,找到注解形式的处理器适配器,对RequestM ...
- SpringMVC post和get乱码解决方式
如何解决POST请求中文乱码问题,GET的又如何处理呢? 在web.xml中加入: <filter> <filter-name>CharacterEncodingFil ...
- SpringMVC问题- MultipartConfig 配置问题以及解决方式
http://www.cnblogs.com/weilu2/p/springmvc_fileupload_with_servlet_3_0.html
- 【转载】SpringMVC前台给后台传值的方式
转自:http://blog.csdn.net/flymoringbird/article/details/53126505 1. 基本数据类型(以int为例,其他类似): Controller代码: ...
- NPOI EXECL数据导入,日期格式调用DateCellValue取值时,二次或后续调用出现报错!
NPOI version:2.5.1 EXCEL数据导入功能,第一次调用DateCellValue获得日期值OK,二次或后续调用出现报错"函数求值需要运行所有线程" 初步怀疑是版本 ...
- 关于Java中文乱码与日期格式
关于Java中文乱码与日期格式 Java中文乱码的问题其实很普遍了,首先,一般不会在Windows平台下面出现,同时,一般在Tomcat应用服务器下也都正常,但是到了WebSphere,Weblogi ...
随机推荐
- iOS crash 崩溃问题的追踪方法
http://www.cnblogs.com/easonoutlook/archive/2012/12/27/2835884.html iOS crash 崩溃问题的追踪方法 在调试程序的时候,总是碰 ...
- Linux内核驱动之延时---内核超时处理【转】
转自:http://blog.chinaunix.net/uid-24219701-id-3288103.html 内核超时处理 jiffies 计数器 定时器中断由系统定时硬件以规律地间隔产生; 这 ...
- 使用aiohttp的一个小例子
#!/usr/bin/env python # encoding: utf-8 import aiohttp import asyncio import traceback import time i ...
- Postman前端HTTP请求调试神器教程
Postman功能: 主要用于模拟网络请求包 快速创建请求 回放.管理请求 快速设置网络代理 我们看下界面: 一 接口请求流程: 二 postman使用 从流程图中我们可以看出,一个接口请求需要设 ...
- springBoot Ribbon 负载均衡
1.依赖引用 <!-- 引入关于 eureka-server的依赖 --> <dependency> <groupId>org.springframework.cl ...
- AC日记——数颜色 bzoj 2120
2120 思路: 带修改的莫队: 对于离线排序询问的算法,如何修改呢? 每个询问添加一个修改标记: 表示当前询问在第几个修改之后: 然后把修改标记作为第三关键字来排序: 每次更新端点,先更新时间: 块 ...
- poj2728(最小比率生成树)
poj2728 题意 给出 n 个点的坐标和它的高度,求一颗生成树使得树上所连边的两点高度差之和除以距离之和最小. 分析 01分数规划+最小生成树. 对于所有的边,在求最小生成树过程中有选或不选的问题 ...
- 【模拟】Friday the Thirteenth
题目描述 Is Friday the 13th really an unusual event?That is, does the 13th of the month land on a Friday ...
- Vue之$set使用
背景 后端参与前端开发的小白,在开发过程中遇到了如下情况:当vue的data里边声明或者已经赋值过的对象或者数组(数组里边的值是对象)时,向对象中添加新的属性,如果更新此属性的值,是不会更新视图的. ...
- Jenkins配置Publish Junit test result report(转)
参考这篇文章:http://www.yiibai.com/jenkins/jenkins_unit_testing.html 插件:JUnit Plugin