1. 程式人生 > >spring mvc使用@InitBinder 標籤對錶單資料繫結

spring mvc使用@InitBinder 標籤對錶單資料繫結

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

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

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


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

import org.springframework.beans.propertyeditors.PropertiesEditor;

public class DoubleEditor extends PropertiesEditor {  
    @Override  
    public void setAsText(String text) throws IllegalArgumentException {  
        if (text == null || text.equals("")) {  
            text = "0";  
        }  
        setValue(Double.parseDouble(text));  
    }  
  
    @Override  
    public String getAsText() {  
        return getValue().toString();  
    }  
} 

import org.springframework.beans.propertyeditors.PropertiesEditor;

public class IntegerEditor extends PropertiesEditor {  
    @Override  
    public void setAsText(String text) throws IllegalArgumentException {  
        if (text == null || text.equals("")) {  
            text = "0";  
        }  
        setValue(Integer.parseInt(text));  
    }  
  
    @Override  
    public String getAsText() {  
        return getValue().toString();  
    }  
} 

import org.springframework.beans.propertyeditors.PropertiesEditor;

public class FloatEditor extends PropertiesEditor {  
    @Override  
    public void setAsText(String text) throws IllegalArgumentException {  
        if (text == null || text.equals("")) {  
            text = "0";  
        }  
        setValue(Float.parseFloat(text));  
    }  
  
    @Override  
    public String getAsText() {  
        return getValue().toString();  
    }  
}  

import org.springframework.beans.propertyeditors.PropertiesEditor;

public class LongEditor extends PropertiesEditor {  
    @Override  
    public void setAsText(String text) throws IllegalArgumentException {  
        if (text == null || text.equals("")) {  
            text = "0";  
        }  
        setValue(Long.parseLong(text));  
    }  
  
    @Override  
    public String getAsText() {  
        return getValue().toString();  
    }  
}  

在BaseController中
	@InitBinder  
    protected void initBinder(WebDataBinder binder) {  
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));  
//        binder.registerCustomEditor(int.class, new CustomNumberEditor(int.class, true));  
        binder.registerCustomEditor(int.class, new IntegerEditor());  
//        binder.registerCustomEditor(long.class, new CustomNumberEditor(long.class, true));
        binder.registerCustomEditor(long.class, new LongEditor());  
        binder.registerCustomEditor(double.class, new DoubleEditor());  
        binder.registerCustomEditor(float.class, new FloatEditor());  
    } 
public class org.springframework.beans.propertyeditors.PropertiesEditor extends java.beans.PropertyEditorSupport {

看到沒?如果你的編輯器類直接繼承PropertyEditorSupport也可以。 先就這樣吧,洗洗啪啪啪。。。