spring mvc使用@InitBinder 標籤對錶單數據綁定

來源

在SpringMVC中,bean中定義了Date,double等類型,如果沒有做任何處理的話,日期以及double都無法綁定。


解決的辦法就是使用spring mvc提供的@InitBinder標籤

在我的項目中是在BaseController中增加方法initBinder,並使用註解@InitBinder標註,那麼spring mvc在綁定表單之前,都會先註冊這些編輯器,當然你如果不嫌麻煩,你也可以單獨的寫在你的每一個controller中。剩下的控制器都繼承該類。spring自己提供了大量的實現類,諸如CustomDateEditor ,CustomBooleanEditor,CustomNumberEditor等許多,基本上夠用。


當然,我們也可以不使用他自己自帶這些編輯器類,那下面我們自己去構造幾個

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. import org.springframework.beans.propertyeditors.PropertiesEditor;  
  2.   
  3. public class DoubleEditor extends PropertiesEditor {    
  4.     @Override    
  5.     public void setAsText(String text) throws IllegalArgumentException {    
  6.         if (text == null || text.equals("")) {    
  7.             text = "0";    
  8.         }    
  9.         setValue(Double.parseDouble(text));    
  10.     }    
  11.     
  12.     @Override    
  13.     public String getAsText() {    
  14.         return getValue().toString();    
  15.     }    
  16. }   

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. import org.springframework.beans.propertyeditors.PropertiesEditor;  
  2.   
  3. public class IntegerEditor extends PropertiesEditor {    
  4.     @Override    
  5.     public void setAsText(String text) throws IllegalArgumentException {    
  6.         if (text == null || text.equals("")) {    
  7.             text = "0";    
  8.         }    
  9.         setValue(Integer.parseInt(text));    
  10.     }    
  11.     
  12.     @Override    
  13.     public String getAsText() {    
  14.         return getValue().toString();    
  15.     }    
  16. }   

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. import org.springframework.beans.propertyeditors.PropertiesEditor;  
  2.   
  3. public class FloatEditor extends PropertiesEditor {    
  4.     @Override    
  5.     public void setAsText(String text) throws IllegalArgumentException {    
  6.         if (text == null || text.equals("")) {    
  7.             text = "0";    
  8.         }    
  9.         setValue(Float.parseFloat(text));    
  10.     }    
  11.     
  12.     @Override    
  13.     public String getAsText() {    
  14.         return getValue().toString();    
  15.     }    
  16. }    

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. import org.springframework.beans.propertyeditors.PropertiesEditor;  
  2.   
  3. public class LongEditor extends PropertiesEditor {    
  4.     @Override    
  5.     public void setAsText(String text) throws IllegalArgumentException {    
  6.         if (text == null || text.equals("")) {    
  7.             text = "0";    
  8.         }    
  9.         setValue(Long.parseLong(text));    
  10.     }    
  11.     
  12.     @Override    
  13.     public String getAsText() {    
  14.         return getValue().toString();    
  15.     }    
  16. }    

在BaseController中

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. @InitBinder    
  2.    protected void initBinder(WebDataBinder binder) {    
  3.        binder.registerCustomEditor(Date.classnew CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));    
  4. /        binder.registerCustomEditor(int.classnew CustomNumberEditor(int.classtrue));    
  5.        binder.registerCustomEditor(int.classnew IntegerEditor());    
  6. /        binder.registerCustomEditor(long.classnew CustomNumberEditor(long.classtrue));  
  7.        binder.registerCustomEditor(long.classnew LongEditor());    
  8.        binder.registerCustomEditor(double.classnew DoubleEditor());    
  9.        binder.registerCustomEditor(float.classnew FloatEditor());    
  10.    }   


[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. public class org.springframework.beans.propertyeditors.PropertiesEditor extends java.beans.PropertyEditorSupport {  

看到沒?如果你的編輯器類直接繼承PropertyEditorSupport也可以。



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