spring mvc绑定对象String转Date
来源:互联网 发布:淘宝男装好的店铺 编辑:程序博客网 时间:2024/05/29 16:21
使用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) : "");
- }
- }
0 0
- spring mvc绑定对象String转Date
- spring mvc绑定对象String转Date
- spring mvc绑定对象String转Date
- spring mvc绑定对象String转Date
- spring mvc绑定对象String转Date
- Spring mvc 数据绑定 String 绑定 Date
- Spring mvc 绑定Date日期
- spring mvc时间string转Date问题(一)
- java spring mvc时间String转Date(二)
- spring mvc时间String转date(三)
- Spring MVC 数据绑定多个对象
- spring mvc 绑定对象 并进行校验
- spring mvc中string不能转为date型解决
- Spring mvc 提交时间 String Date 的转换问题
- spring mvc3(对象传参 Date 类型数据绑定)
- spring mvc String类型转为一个对象
- Spring MVC 数据绑定 之 对象里面有对象系列
- java bean date string绑定
- 【Flume】【源码分析】flume中FailoverSinkProcessor容错处理机制源码分析
- 现金流 for ipad
- java获取文件Mime Type的几种方式
- 程序员想打基础看那几本书
- 一步一步写算法(之单向链表)
- spring mvc绑定对象String转Date
- linux内核设计与实现读书笔记——页高速缓存
- aptitude和apt-get的区别和联系【转,有添加和修改】
- ##* %%* linux变量处理
- 产品经理学习笔记(3)-腾讯亿级用户的团队经验:产品经理如何协同工作
- 1501080931-蓝桥杯-算法训练 排序
- 【机器学习算法班】(一)管窥机器学习
- 一步一步写算法(之双向链表)
- Linux进程关系