1. 程式人生 > >spring mvc使用@InitBinder 標簽對表單數據綁定

spring mvc使用@InitBinder 標簽對表單數據綁定

使用註解 highlight ref sta float tab 基本上 orm board

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

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

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

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

[java] view plain copy
  1. import org.springframework.beans.propertyeditors.PropertiesEditor;
  2. public class DoubleEditor extends PropertiesEditor {
  3. @Override
  4. public void setAsText(String text) throws IllegalArgumentException {
  5. if (text == null || text.equals("")) {
  6. text = "0";
  7. }
  8. setValue(Double.parseDouble(text));
  9. }
  10. @Override
  11. public String getAsText() {
  12. return getValue().toString();
  13. }
  14. }

[java] view plain copy
  1. import org.springframework.beans.propertyeditors.PropertiesEditor;
  2. public class IntegerEditor extends PropertiesEditor {
  3. @Override
  4. public void setAsText(String text) throws IllegalArgumentException {
  5. if (text == null || text.equals("")) {
  6. text = "0";
  7. }
  8. setValue(Integer.parseInt(text));
  9. }
  10. @Override
  11. public String getAsText() {
  12. return getValue().toString();
  13. }
  14. }

[java] view plain copy
  1. import org.springframework.beans.propertyeditors.PropertiesEditor;
  2. public class FloatEditor extends PropertiesEditor {
  3. @Override
  4. public void setAsText(String text) throws IllegalArgumentException {
  5. if (text == null || text.equals("")) {
  6. text = "0";
  7. }
  8. setValue(Float.parseFloat(text));
  9. }
  10. @Override
  11. public String getAsText() {
  12. return getValue().toString();
  13. }
  14. }

[java] view plain copy
  1. import org.springframework.beans.propertyeditors.PropertiesEditor;
  2. public class LongEditor extends PropertiesEditor {
  3. @Override
  4. public void setAsText(String text) throws IllegalArgumentException {
  5. if (text == null || text.equals("")) {
  6. text = "0";
  7. }
  8. setValue(Long.parseLong(text));
  9. }
  10. @Override
  11. public String getAsText() {
  12. return getValue().toString();
  13. }
  14. }


在BaseController中

[java] view plain copy
  1. @InitBinder
  2. protected void initBinder(WebDataBinder binder) {
  3. binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
  4. / binder.registerCustomEditor(int.class, new CustomNumberEditor(int.class, true));
  5. binder.registerCustomEditor(int.class, new IntegerEditor());
  6. / binder.registerCustomEditor(long.class, new CustomNumberEditor(long.class, true));
  7. binder.registerCustomEditor(long.class, new LongEditor());
  8. binder.registerCustomEditor(double.class, new DoubleEditor());
  9. binder.registerCustomEditor(float.class, new FloatEditor());
  10. }

[java] view plain copy
    1. public class org.springframework.beans.propertyeditors.PropertiesEditor extends java.beans.PropertyEditorSupport {

spring mvc使用@InitBinder 標簽對表單數據綁定