spring mvc绑定对象String转Date解决入参不能是Date的问题
使用spring的mvc,直接将页面参数绑定到对象中,对象中有属性为Date时会报错,此时需要处理下。
同样的,其他的需要处理的类型也可以用这种方法。
在controller中加入代码
@InitBinder
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
//对于需要转换为Date类型的属性,使用DateEditor进行处理
binder.registerCustomEditor(Date.class, new DateEditor(TIMEFORMAT, true));
}
DateEditor为自定义的处理类,继承自PropertyEditorSupport,处理方法为public void setAsText(String text) throws IllegalArgumentException
package com.elong.activity.web.filter; import java.beans.PropertyEditorSupport;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; import com.elong.common.util.StringUtils; /**
*
* (入参转化)
*
* <p>
* 修改历史: <br>
* 修改日期 修改人员 版本 修改内容<br>
* -------------------------------------------------<br>
* 2015年6月15日 下午6:16:17 user 1.0 初始化创建<br>
* </p>
*
* @author Peng.Li
* @version 1.0
* @since JDK1.7
*/
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.isBlank(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) : "");
} }
第二种是使注解的方式:
import org.springframework.format.annotation.DateTimeFormat;
/**
* 入住日期
*/
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
private Date checkInTime;
/**
* 离店日期
*/
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
private Date checkOutTime;
详细说明见博客:http://relive123-yahoo-com-cn.iteye.com/blog/1678376
spring mvc绑定对象String转Date解决入参不能是Date的问题的更多相关文章
- spring MVC模式拦截所有入口方法的入参出参打印
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SerializerFeature; im ...
- spring mvc绑定参数之 类型转换 有三种方式:
spring mvc绑定参数之类型转换有三种方式: 1.实体类中加日期格式化注解(上次做项目使用的这种.简单,但有缺点,是一种局部的处理方式,只能在本实体类中使用.方法三是全局的.) @DateTim ...
- springmvc中将servlet api对象作为处理方法的入参使用
在springmvc中,控制器不依赖任何servlet api对象,也可以将servlet api对象作为处理方法的入参使用,非常方便,比如需要使用HttpSession对象,那么就可以直接将Http ...
- 【spring mvc】后台API查询接口,get请求,后台Date字段接收前台String类型的时间,报错default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createDate';
后台API查询接口,get请求,后台Date字段接收前台String类型的时间筛选条件 后台接口接收 使用的实体 而createDate字段在后台实体中是Date类型 报错信息: org.spring ...
- Spring @ResponseBody只能返回String类型数据解决办法
今天自己搭Spring MVC框架玩,使用AJAX调用Spring controller 并返回map对象,突然发现,哎,怎么@Response中只能返回String, 我用的Spring 3的版本也 ...
- Spring Mvc返回html页面404错误解决记录--转载
原文地址:http://53873039oycg.iteye.com/blog/2061992 以前使用Spring Mvc时候都是返回jsp页面或者ftl页面,昨天想返回html页面,spring- ...
- 前台ajax传参数,后台spring mvc用对象接受
第二种方法:利用spring mvc的机制,调用对象的get方法,要求对象的属性名和传的参数名字一致(有兴趣的同学看 springmvc源码) 1.将参数名直接写成对象的属性名 $.ajax({ ur ...
- spring mvc:日志对象logger的复用
在采用Spring mvc+org.slf4j.Logger开发项目时,发现几乎每个controller或者manager都有的一个标配: private final static Logger LO ...
- Spring MVC传输对象属性
今天搬砖时遇到一个问题,前端使用JSP+form传输数据,后台使用Spring MVC接收,但是接收到的对象属性一直是null,找了好久才发现原因,代码如下 前端代码 后端代码 需要注意一点 ...
随机推荐
- hdu 1434 幸福列车
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1434 幸福列车 Description 一批幸福的列车即将从杭州驶向幸福的终点站——温州,身为总列车长 ...
- c中static作用
1. static 变量 静态变量的类型 说明符是static. 静态变量当然是属于静态存储方式,但是属于静态存储方式的量不一定就是静态变量. 例如外部变量虽属于静态 存储方式,但不一定是静态变量 ...
- gcc 创建库及使用
gcc -shared hello.c -o libhello.so gcc -o test test.c -L库所在目录 -l库名
- java笔试题(2)
简述构造器的运行机制 首先要注意的是的构造器并不是函数,所以他并不能被继承,这在我们extends的时候写子类的构造器时比较的常见,即使子类构造器参数和父类的完全一样,我们也要写super就是因为这个 ...
- PHP错误The server encountered an internal error or misconfiguration and was unable to complete your re
我的笔记本电脑上的环境安装了很多次,但是运行项目时总是会报The server encountered an internal error or misconfiguration and was un ...
- nodejs笔记一--模块,全局process对象;
一.os模块可提供操作系统的一些基本信息,它的一些常用方法如下: var os = require("os"); var result = os.platform(); //查看操 ...
- liferay MVCActionCommand的用法及例子
在liferay7中把portlet中的控制层拆成了3个部分: 1.MVCActionCommand 2.MVCRenderCommand 3.MVCRecourceCommand 至于为什么要拆出来 ...
- 【转】GCC编译使用动态链接库
相关gcc参数:-l -L -shared -fPIC -static -c -o 原文地址:[脚本之家]http://www.jb51.net/article/34990.htm 根据链 ...
- 搜索 基础 AC 2014-01-14 15:53 170人阅读 评论(0) 收藏
题目网址:http://haut.openjudge.cn/xiyoulianxi1/1/ 1:晶矿的个数 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 ...
- 我的这10年——从机械绘图 到 炼油 到 微软MVP 的华丽转身
年底了,各种总结计划满天飞,有空的时候我也一直在思考这么多年,是怎么过来的.也曾经很迷茫,希望经验和经历能给大家一点带来一点正能量的东西.10年很长,10年前说实话我没有思考过现在的样子,但10年前的 ...