`

spring mvc绑定对象String转Date

 
阅读更多

学习笔记,转自:http://blog.csdn.net/whumr1/article/details/8056285

 

使用spring的mvc,直接将页面参数绑定到对象中,对象中有属性为Date时会报错,此时需要处理下。

同样的,其他的需要处理的类型也可以用这种方法。

在controller中加入代码

 

[java] view plaincopy
  1. @InitBinder  
  2. protected void initBinder(HttpServletRequest request,  
  3.                               ServletRequestDataBinder binder) throws Exception {  
  4.     //对于需要转换为Date类型的属性,使用DateEditor进行处理  
  5.     binder.registerCustomEditor(Date.classnew DateEditor());  
  6. }  


DateEditor为自定义的处理类,继承自PropertyEditorSupport,处理方法为public void setAsText(String text) throws IllegalArgumentException

 

 

[java] view plaincopy
  1. import org.springframework.util.StringUtils;  
  2.   
  3. import java.beans.PropertyEditorSupport;  
  4. import java.text.DateFormat;  
  5. import java.text.ParseException;  
  6. import java.text.SimpleDateFormat;  
  7. import java.util.Date;  
  8.   
  9. public class DateEditor extends PropertyEditorSupport {  
  10.   
  11.     private static final DateFormat DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd");  
  12.     private static final DateFormat TIMEFORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  13.   
  14.     private DateFormat dateFormat;  
  15.     private boolean allowEmpty = true;  
  16.   
  17.     public DateEditor() {  
  18.     }  
  19.   
  20.     public DateEditor(DateFormat dateFormat) {  
  21.         this.dateFormat = dateFormat;  
  22.     }  
  23.   
  24.     public DateEditor(DateFormat dateFormat, boolean allowEmpty) {  
  25.         this.dateFormat = dateFormat;  
  26.         this.allowEmpty = allowEmpty;  
  27.     }  
  28.   
  29.     /** 
  30.      * Parse the Date from the given text, using the specified DateFormat. 
  31.      */  
  32.     @Override  
  33.     public void setAsText(String text) throws IllegalArgumentException {  
  34.         if (this.allowEmpty && !StringUtils.hasText(text)) {  
  35.             // Treat empty String as null value.  
  36.             setValue(null);  
  37.         } else {  
  38.             try {  
  39.                 if(this.dateFormat != null)  
  40.                     setValue(this.dateFormat.parse(text));  
  41.                 else {  
  42.                     if(text.contains(":"))  
  43.                         setValue(TIMEFORMAT.parse(text));  
  44.                     else  
  45.                         setValue(DATEFORMAT.parse(text));  
  46.                 }  
  47.             } catch (ParseException ex) {  
  48.                 throw new IllegalArgumentException("Could not parse date: " + ex.getMessage(), ex);  
  49.             }  
  50.         }  
  51.     }  
  52.   
  53.     /** 
  54.      * Format the Date as String, using the specified DateFormat. 
  55.      */  
  56.     @Override  
  57.     public String getAsText() {  
  58.         Date value = (Date) getValue();  
  59.         DateFormat dateFormat = this.dateFormat;  
  60.         if(dateFormat == null)  
  61.             dateFormat = TIMEFORMAT;  
  62.         return (value != null ? dateFormat.format(value) : "");  
  63.     }  
  64. }  


 

分享到:
评论

相关推荐

    spring MVC数据绑定大全

    spring MVC数据绑定 含例子 转载自疯芒毕露的专栏 刚开始用spring mvc 做web开发时 经常会不知道如何合适绑定页面数据 用惯struts2的朋友更认为spring mvc 绑定数据不如struts2方便 本人最开始也是这么认为 经过一段...

    精通Spring MVC 4

    Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。Spring MVC4是当前zuixin的版本,在众多特性上有了进一步的提升。, 在精通Spring...

    spring mvc 参数绑定漏洞

    NULL 博文链接:https://yfm049.iteye.com/blog/860494

    SpringMVCDemo:Spring MVC 框架知识案例

    4.Spring MVC 域对象共享数据案例 5.Spring MVC @ModelAttribute 注解案例 6.Spring MVC 国际化案例 7.Spring MVC 请求转发与请求重定向案例 8.Spring MVC 下载 Excel 文档的需求案例 9.Spring MVC RESTful 风格的...

    Spring+MVC数据绑定大全+

    Spring+MVC数据绑定大全+

    [免费]Spring MVC学习指南(高清)

    全书共计12章,分别从Spring框架、模型2和MVC模式、Spring MVC介绍、控制器、数据绑定和表单标签库、传唤器和格式化、验证器、表达式语言、JSTL、国际化、上传文件、下载文件多个角度介绍了Spring MVC。除此之外,...

    spring mvc 官方文档

    本文详细介绍spring MVC的原理和开发心得体会。

    精通Spring MVC 4 中文

    精通Spring MVC 4 中文

    大优惠 Spring MVC学习指南(第2版)2017.pdf

    全书共计12章,分别从Spring框架、模型2和MVC模式、Spring MVC介绍、控制器、数据绑定和表单标签库、传唤器和格式化、验证器、表达式语言、JSTL、国际化、上传文件、下载文件多个角度介绍了Spring MVC。除此之外,...

    Spring MVC 基于注解实例

    Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于...

    Spring MVC+MyBatis开发从入门到项目实战

    第3篇是Spring MVC技术入门,包括Spring MVC的背景介绍、架构整体剖析、环境搭建、处理器与映射器的讲解、前端控制器的源码分析、多种视图解析器的介绍、请求映射与参数绑定的介绍、Validation校验与异常处理和拦截...

    Spring MVC 入门实例

    首先, 我需要在你心里建立起 Spring MVC 的基本概念. 基于 Spring 的 Web 应用程序接收到 http://localhost:8080/hello.do(事实上请求路径是 /hello.do) 的请求后, Spring 将这个请求交给一个名为 helloController ...

    Spring MVC所需jar包

    Spring MVC所需jar包,包含java开发中 Spring MVC架构中最常用的jar包

    Spring MVC jar包

    关于构建Sping MVC的Jar包,包括Sping2.5.6和Hibernate3.6.8

    spring mvc源代码

    spring mvc4.1.4 源代码 spring mvc4.1.4 源代码spring mvc4.1.4 源代码spring mvc4.1.4 源代码spring mvc4.1.4 源代码

    Spring MVC 4.2.3

    Spring mvc jar包

    Servlet JSP和Spring MVC初学指南

    Servlet JSP和Spring MVC初学指南

    Spring MVC MyBatis开发从入门到项目实战

    Spring MVC MyBatis开发从入门到项目实战

Global site tag (gtag.js) - Google Analytics