spring mvc绑定对象String转Date(來自http://blog.csdn.net/whumr1/article/details/8056285)

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

同样的,其他的需要处理的类型也可以用这种方法。(其他类型沒有時間研究,歡迎技術牛人補充)

1、在controller中加入代码

@InitBinder
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
//对于需要转换为Date类型的属性,使用DateEditor进行处理
binder.registerCustomEditor(Date.class, new DateEditor());
}

2、DateEditor为自定义的处理类(util),继承自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) : "");
}
}

时间: 2024-07-30 02:04:56

spring mvc绑定对象String转Date(來自http://blog.csdn.net/whumr1/article/details/8056285)的相关文章

spring mvc绑定对象String转Date解决入参不能是Date的问题

使用spring的mvc,直接将页面参数绑定到对象中,对象中有属性为Date时会报错,此时需要处理下. 同样的,其他的需要处理的类型也可以用这种方法. 在controller中加入代码 @InitBinder protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { //对于需要转换为Date类型的属性,使用DateEditor进行处理 bin

as3 页游中,新手指导中,屏蔽所有交互对象,但除了指定交互对象可用的方法【转http://blog.csdn.net/linjf520/article/details/9450945】

package { import flash.display.InteractiveObject; import flash.display.Stage; import flash.events.MouseEvent; /** * 新手指导管理器 * @author jave.lin * @date 2013-7-24 */ public class GuideManager{ private static var stage:Stage; /**设置舞台*/ public static fun

spring加载hibernate映射文件的几种方式。转自:http://blog.csdn.net/huiwenjie168/article/details/7013618

在Spring的applicationContext.xml中配置映射文件,通常是在<sessionFactory>这个Bean实例中进行的,若配置的映射文件较少时,可以用sessionFactory的所属类LocalSessionFactoryBean的“mappingResources”属性,方式包括(mappingResources,mappingLocations.mappingDirectoryLocations与mappingJarLocations )定义方法如下: 第一种: &

asp.net mvc(模式)和三层架构(BLL、DAL、Model)的联系与区别 转载自:http://blog.csdn.net/luoyeyu1989/article/details/8275866

首先,MVC和三层架构,是不一样的. 三层架构中,DAL(数据访问层).BLL(业务逻辑层).WEB层各司其职,意在职责分离. MVC是 Model-View-Controller,严格说这三个加起来以后才是三层架构中的WEB层,也就是说,MVC把三层架构中的WEB层再度进行了分化,分成了控制器.视图.实体三个部分,控制器完成页面逻辑,通过实体来与界面层完成通话:而C层直接与三层中的BLL进行对话. 所以, .net的三层结构中,并没有action这个概念. asp.net mvc 是微软新发布

反射给对象赋值遇到的问题——类型转换[转http://blog.csdn.net/xiaohan2826/article/details/8536074]

发布时间:2012-10-25 10:49浏览次数:225 给一个对象属性赋值可以通过PropertyInfo.SetValue()方式进行赋值,但要注意值的类型要与属性保持一致. 创建对象实例的两种方法: 1. 1 var obj = Assembly.Load("AssemblyName").CreateInstance("AssemblyName"+"ClassFullName"); 2. 1 var obj = Activator.Cre

spring的事务管理有几种方式实现 (转自:http://blog.csdn.net/bopzhou/article/details/7094108)

spring的事务管理有几种方式实现 标签: springhibernate数据库beanlistclass 2011-12-22 09:12 3883人阅读 评论(0) 收藏 举报 Spring+Hibernate的实质: 就是把Hibernate用到的数据源Datasource,Hibernate的SessionFactory实例,事务管理器HibernateTransactionManager,都交给Spring管理. 那么再没整合之前Hibernate是如何实现事务管理的呢? 通过Ser

SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)转载(http://blog.csdn.net/zhshulin/article/details/23912615)

这两天需要用到MyBatis的代码自动生成的功能,由于MyBatis属于一种半自动的ORM框架,所以主要的工作就是配置Mapping映射文件,但是由于手写映射文件很容易出错,所以可利用MyBatis生成器自动生成实体类.DAO接口和Mapping映射文件.这样可以省去很多的功夫,将生成的代码copy到项目工程中即可. 使用自动生成有很多方式,可以在eclipse中安装插件,但是以下将要介绍的这种方式我认为很轻松,最简单,不需要装插件,只需要下几个jar包即可,把它们放在一个目录下面. 生成代码需

spring -mvc 将对象封装json返回时删除掉对象中的属性注解方式

spring -mvc 将对象封装json返回时删除掉对象中的属性注解方式 在类名,接口头上注解使用在 @JsonIgnoreProperties(value={"comid"}) //希望动态过滤掉的属性 例 @JsonIgnoreProperties(value={"comid"}) public interface 接口名称{ } @JsonIgnoreProperties(value={"comid"}) public class 类名{

spring mvc:日志对象logger的复用

在采用Spring mvc+org.slf4j.Logger开发项目时,发现几乎每个controller或者manager都有的一个标配: private final static Logger LOGGER = LoggerFactory.getLogger(Test.class); 看起来并没有什么问题,但是毫无疑问的是,每新建一个controller或者manager就需要写一次几乎相同的代码会间接降低效率,另外因为每个controller或者manager都有一个这样的日志对象,无疑会增