spring mvc使用@InitBinder標簽對表單數(shù)據(jù)綁定的方法
在SpringMVC中,bean中定義了Date,double等類型,如果沒有做任何處理的話,日期以及double都無法綁定。
解決的辦法就是使用spring mvc提供的@InitBinder標簽
在我的項目中是在BaseController中增加方法initBinder,并使用注解@InitBinder標注,那么spring mvc在綁定表單之前,都會先注冊這些編輯器,當然你如果不嫌麻煩,你也可以單獨的寫在你的每一個controller中。剩下的控制器都繼承該類。spring自己提供了大量的實現(xiàn)類,諸如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 {
看到?jīng)]?如果你的編輯器類直接繼承PropertyEditorSupport也可以。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
mybatis通過TypeHandler?list轉換string類型轉換方式
這篇文章主要介紹了mybatis通過TypeHandler?list轉換string類型轉換方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
springmvc+spring+mybatis實現(xiàn)用戶登錄功能(上)
這篇文章主要為大家詳細介紹了springmvc+spring+mybatis實現(xiàn)用戶登錄功能,比較基礎的學習教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
Springboot實現(xiàn)前后端分離excel下載
這篇文章主要介紹了Springboot實現(xiàn)前后端分離excel下載,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
Java文件下載ZIP報錯:Out of Memory的問題排查
本文主要介紹了Java項目中下載大文件(超過2G的ZIP文件)時出現(xiàn)內存溢出(OutOfMemory:JavaHeapSpace)的問題,具有一定的參考價值,感興趣的可以了解一下2025-01-01
SpringBoot中的Condition包下常用條件依賴注解案例介紹
這篇文章主要介紹了SpringBoot中的Condition包下常用條件依賴注解案例,文章基于Java的相關資料展開主題詳細內容,需要的小伙伴可以參考一下2022-04-04

