spring mvc绑定对象String转Date

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

同样的,其他的需要处理的类型也可以用这种方法。

在controller中加入代码

[java]  view plain  copy
  1. @InitBinder  
  2. protected void initBinder(HttpServletRequest request,  
  3.                               ServletRequestDataBinder binder) throws Exception {  
  4.     //对于需要转换为Date类型的属性,使用DateEditor进行处理  
  5.     binder.registerCustomEditor(Date.classnew DateEditor());  
  6. }  

DateEditor为自定义的处理类,继承自PropertyEditorSupport,处理方法为public void setAsText(String text) throws IllegalArgumentException

[java]  view plain  copy
  1. import org.springframework.util.StringUtils;  
  2.   
  3. import java.beans.PropertyEditorSupport;  
  4. import java.text.DateFormat;  
  5. import java.text.ParseException;  
  6. import java.text.SimpleDateFormat;  
  7. import java.util.Date;  
  8.   
  9. public class DateEditor extends PropertyEditorSupport {  
  10.   
  11.     private static final DateFormat DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd");  
  12.     private static final DateFormat TIMEFORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  13.   
  14.     private DateFormat dateFormat;  
  15.     private boolean allowEmpty = true;  
  16.   
  17.     public DateEditor() {  
  18.     }  
  19.   
  20.     public DateEditor(DateFormat dateFormat) {  
  21.         this.dateFormat = dateFormat;  
  22.     }  
  23.   
  24.     public DateEditor(DateFormat dateFormat, boolean allowEmpty) {  
  25.         this.dateFormat = dateFormat;  
  26.         this.allowEmpty = allowEmpty;  
  27.     }  
  28.   
  29.     /** 
  30.      * Parse the Date from the given text, using the specified DateFormat. 
  31.      */  
  32.     @Override  
  33.     public void setAsText(String text) throws IllegalArgumentException {  
  34.         if (this.allowEmpty && !StringUtils.hasText(text)) {  
  35.             // Treat empty String as null value.  
  36.             setValue(null);  
  37.         } else {  
  38.             try {  
  39.                 if(this.dateFormat != null)  
  40.                     setValue(this.dateFormat.parse(text));  
  41.                 else {  
  42.                     if(text.contains(":"))  
  43.                         setValue(TIMEFORMAT.parse(text));  
  44.                     else  
  45.                         setValue(DATEFORMAT.parse(text));  
  46.                 }  
  47.             } catch (ParseException ex) {  
  48.                 throw new IllegalArgumentException("Could not parse date: " + ex.getMessage(), ex);  
  49.             }  
  50.         }  
  51.     }  
  52.   
  53.     /** 
  54.      * Format the Date as String, using the specified DateFormat. 
  55.      */  
  56.     @Override  
  57.     public String getAsText() {  
  58.         Date value = (Date) getValue();  
  59.         DateFormat dateFormat = this.dateFormat;  
  60.         if(dateFormat == null)  
  61.             dateFormat = TIMEFORMAT;  
  62.         return (value != null ? dateFormat.format(value) : "");  
  63.     }  
  64. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值