spring mvc綁定對象String轉Date

使用spring的mvc,直接將頁面參數綁定到對象中,對象中有屬性爲Date時會報錯,此時需要處理下。

同樣的,其他的需要處理的類型也可以用這種方法。

在controller中加入代碼

  1. @InitBinder  

  2. protected void initBinder(HttpServletRequest request,  

  3.                               ServletRequestDataBinder binder) throws Exception {  

  4.     //對於需要轉換爲Date類型的屬性,使用DateEditor進行處理  

  5.     binder.registerCustomEditor(Date.classnew DateEditor());  

  6. }  

  7. @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

  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. }

參考來源 spring mvc綁定對象String轉Date

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章