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,找了好久才发现原因,代码如下 前端代码 后端代码 需要注意一点 ...
随机推荐
- 11G ORACLE RAC DBCA 无法识别asm磁盘组
ASM磁盘无法识别几种现象: 1) gi家目录或者其子目录权限错误 2)asm磁盘的权限错误 3)asm实例未启动或者asm磁盘组没有mount上 4)asm磁盘组资源没有在线 5)oracle用户的 ...
- Android:简单实现ViewPager+TabHost+TabWidget实现导航栏导航和滑动切换
viewPager是v4包里的一个组件,可以实现滑动显示多个界面. android也为viewPager提供了一个adapter,此adapter最少要重写4个方法: public int getCo ...
- Android or iOS 运行 meteor App 屏幕一片空白 White screen的解决方法
在mac上出现这种错误,多是与文件夹的权限有关,有人建议把~/.meteor目录删除,重新下载安装.在墙内重新下载安装的代价非常之大. 简单的解决方法,便是把~/.meteor,以及当前项目目录的权限 ...
- 一个.net程序员教你使用less
我是一个.net 程序员,虽然说一直做后台,但是web 前端也会去学,虽然说技术只是层窗户纸,但是像我这种多动症患者,不捅破我心难受啊! 好!废话不多提,下面直接正题,至于less 是什么这里不多讲因 ...
- Mac下显示\隐藏所有文件
显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true隐藏Mac隐藏文件的命令:defaults write ...
- Timer Design in StatusBar
Timer in StatusBar we need to show local time in StatusBar. solution: 1. add textblock control 2. bi ...
- 89C51单片机实现的流水灯
/*******************************************Copyright: 2014.02.09.version1.0File name: led.cDescrip ...
- 在package.json中配置Script执行npm run tslint报错问题
今天在学习tslint的时候,按照git clone下angular2-webpack-starter的代码执行npm run lint时,虽然代码进行了检测,但检测完成后npm始终报错, //pac ...
- 基于.net mvc的校友录(五、web.config对的配置以及filter实现的权限控制)
web.config配置文件 此文件是整个系统的配置中心,它告诉iis服务器本网站需要哪些运行时环境,需要哪些环境,将要进行哪些操作,开发人员也会将一个常量性的数据放在此配置中,以备系统全局调用.此文 ...
- Tomcat配置HTTPS方式生成安全证书
在Tomcat 6中配置SSL双向认证是相当容易的,本文将介绍如何使用JDK的keytool来为Tomcat配置双向SSL认证.并实现批量生成证书 系统需求:JDK 5.0Tomcat 6.0.16启 ...