原文 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概述--依赖注入--JDBC、事务支持,Bean管理--AOP--Spring MVC1--Sprng MVC2;以及一个新闻发布系统例子。
里面有 Sprng2.5.6+Hibernate3.6+Struts2.3.16的Jar包 已经分好类 但是strut2里面只是包含了最基础的Jar包,其他的按需自己+。 里面还有 jstl标签库的jar 和 数据库MySQL的驱动包 详细配置方法: ...
NULL 博文链接:https://ylsuccess.iteye.com/blog/451836
Spring和Struts都是近年来比较流行的框架,Struts主要用于表示层,Spring用于业务层,以及Hiberate主要用于持久层,
spring配置 spring配置 spring配置 spring配置 spring配置
spring4.0+mybatis3.2+springMvc+easyui1.3.2兼容IE8,内含权限管理demo
访问的网站慢 的原因 讲述了好多访问你网站慢的原因及解决的好多方法
Spring运行helloworld所需的三个基础包,分明为com.springsource.org.apache.commons.logging-1.1.1.jar,spring-beans-5.0.2.RELEASE.jar,spring-core-5.0.2.RELEASE.jar
这里是自己结合spring项目的配置。按照上面的配置可以实现生产发送消息。消费者接受消息。分类设计等
Sprng2.0 核心技术与最佳实践源码(无lib包)
spring框架简介:介绍sprng发展历程、优势、体系结构、概述
spring框架简介,介绍sprng发展历程、优势、体系结构、概述
作用: 1、可以将几个系统的权限都由该系统管理(用户只能管理用户所属组内的角色和权限) 2、可以在此系统框架上开发其他业务逻辑 3、struts2+sprng+hibernate+ajax+html+mysql
资源配置Sprng.xml的重要文件 看到网上资源很少 就给大家分享一下
java生成二维码和条形码(一维码),每个工具类自带main方法,改一下包名和生成路径即可测试。本人使用SpringBoot开发,打包上传的资源
该文档讲解有关Spring的各种开发应该,对学习Sprng的人很有帮助
这个是SprngCloud商城相关文档,上传到svn上面,方便自己学习查看。里边包含了相关比较稳定等资料
sprng项目,实现对零件采购相关流程的管理。采用ssm架构即spring,springmvc,mybatis,数据库采用mysql,前端为layui。
主要介绍了SpringCloud消息总线Bus配置中心实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
sprng boot多个sqlserver数据库或者mysql数据库 数据源配置及事物处理,源码开箱即用,数据库脚本很简单,自己新建库和表,一个name字段即可