学习笔记,转自:http://blog.csdn.net/whumr1/article/details/8056285
使用spring的mvc,直接将页面参数绑定到对象中,对象中有属性为Date时会报错,此时需要处理下。
同样的,其他的需要处理的类型也可以用这种方法。
在controller中加入代码
- @InitBinder
- protected void initBinder(HttpServletRequest request,
- ServletRequestDataBinder binder) throws Exception {
- //对于需要转换为Date类型的属性,使用DateEditor进行处理
- binder.registerCustomEditor(Date.class, new DateEditor());
- }
DateEditor为自定义的处理类,继承自PropertyEditorSupport,处理方法为public void setAsText(String text) throws IllegalArgumentException
- import org.springframework.util.StringUtils;
- import java.beans.PropertyEditorSupport;
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- public class DateEditor extends PropertyEditorSupport {
- private static final DateFormat DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd");
- private static final DateFormat TIMEFORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- private DateFormat dateFormat;
- private boolean allowEmpty = true;
- public DateEditor() {
- }
- public DateEditor(DateFormat dateFormat) {
- this.dateFormat = dateFormat;
- }
- public DateEditor(DateFormat dateFormat, boolean allowEmpty) {
- this.dateFormat = dateFormat;
- this.allowEmpty = allowEmpty;
- }
- /**
- * Parse the Date from the given text, using the specified DateFormat.
- */
- @Override
- public void setAsText(String text) throws IllegalArgumentException {
- if (this.allowEmpty && !StringUtils.hasText(text)) {
- // Treat empty String as null value.
- setValue(null);
- } else {
- try {
- if(this.dateFormat != null)
- setValue(this.dateFormat.parse(text));
- else {
- if(text.contains(":"))
- setValue(TIMEFORMAT.parse(text));
- else
- setValue(DATEFORMAT.parse(text));
- }
- } catch (ParseException ex) {
- throw new IllegalArgumentException("Could not parse date: " + ex.getMessage(), ex);
- }
- }
- }
- /**
- * Format the Date as String, using the specified DateFormat.
- */
- @Override
- public String getAsText() {
- Date value = (Date) getValue();
- DateFormat dateFormat = this.dateFormat;
- if(dateFormat == null)
- dateFormat = TIMEFORMAT;
- return (value != null ? dateFormat.format(value) : "");
- }
- }
相关推荐
spring MVC数据绑定 含例子 转载自疯芒毕露的专栏 刚开始用spring mvc 做web开发时 经常会不知道如何合适绑定页面数据 用惯struts2的朋友更认为spring mvc 绑定数据不如struts2方便 本人最开始也是这么认为 经过一段...
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。Spring MVC4是当前zuixin的版本,在众多特性上有了进一步的提升。, 在精通Spring...
NULL 博文链接:https://yfm049.iteye.com/blog/860494
4.Spring MVC 域对象共享数据案例 5.Spring MVC @ModelAttribute 注解案例 6.Spring MVC 国际化案例 7.Spring MVC 请求转发与请求重定向案例 8.Spring MVC 下载 Excel 文档的需求案例 9.Spring MVC RESTful 风格的...
Spring+MVC数据绑定大全+
全书共计12章,分别从Spring框架、模型2和MVC模式、Spring MVC介绍、控制器、数据绑定和表单标签库、传唤器和格式化、验证器、表达式语言、JSTL、国际化、上传文件、下载文件多个角度介绍了Spring MVC。除此之外,...
本文详细介绍spring MVC的原理和开发心得体会。
精通Spring MVC 4 中文
全书共计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 基于...
第3篇是Spring MVC技术入门,包括Spring MVC的背景介绍、架构整体剖析、环境搭建、处理器与映射器的讲解、前端控制器的源码分析、多种视图解析器的介绍、请求映射与参数绑定的介绍、Validation校验与异常处理和拦截...
首先, 我需要在你心里建立起 Spring MVC 的基本概念. 基于 Spring 的 Web 应用程序接收到 http://localhost:8080/hello.do(事实上请求路径是 /hello.do) 的请求后, Spring 将这个请求交给一个名为 helloController ...
Spring MVC所需jar包,包含java开发中 Spring MVC架构中最常用的jar包
关于构建Sping MVC的Jar包,包括Sping2.5.6和Hibernate3.6.8
spring mvc4.1.4 源代码 spring mvc4.1.4 源代码spring mvc4.1.4 源代码spring mvc4.1.4 源代码spring mvc4.1.4 源代码
Spring mvc jar包
Servlet JSP和Spring MVC初学指南
Spring MVC MyBatis开发从入门到项目实战