springMVC 前后台日期格式传值解决方式之二(共二) @InitBinder的使用
2024-09-05 07:38:57
关于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源码中怎么写的。
最新文章
- swift与OC之间不得不知道的21点
- 无废话WCF
- css问题 ie7兼容性问题
- 基本语法 protocols Category extension
- jquery实现整屏翻屏效果:jquery.mousewheel(一)
- 一款不错的多选下拉列表利器—— Ext.ux.form.SuperBoxSelect
- 面向切面编程AOP:基于XML文件的配置
- 两个EXCEL文件对比去重
- Linux-Hostname-details
- LAMP+LNMP视频教程
- js截取指定字节长度的字符串
- supervisor---------------------------------常用
- SharePoint webpart中悬浮窗口的webconfig路径
- ActiveReports 9 新功能:创新的设计分层报告
- SEO配置信息操作文档
- 判断文件是否存在,不要用if exist和if not exist,因为他们会受到文件是否隐藏的影响,改用dir /a 命令代替
- Java学习笔记之——继承
- python之https爬虫出现 SSL: CERTIFICATE_VERIFY_FAILED (同时打开fiddler就会出现)
- 探索未知种族之osg类生物---呼吸分解之事件循环二
- storm介绍,核心组件,编程模型
热门文章
- ios 改变图片大小缩放方法
- python 拼图验证码
- python的tips:字符和字符串的问题
- php7简短而安全的数组遍历方法
- 51nod 1004 n^n的末位数字【快速幂】
- Nginx的server为0.0.0.0/0.0.0.1的作用?
- 解析HTML文件 - 运用SgmlReader类来解析HTML文件
- samba 服务实现在windows共享文件
- Linux&;Windows下批量修改文件后缀
- 【Hadoop】Apache Eagle 简介--分布式实时 Hadoop 数据安全方案