spring mvc使用@InitBinder標(biāo)簽對(duì)表單數(shù)據(jù)綁定的方法
在SpringMVC中,bean中定義了Date,double等類型,如果沒(méi)有做任何處理的話,日期以及double都無(wú)法綁定。
解決的辦法就是使用spring mvc提供的@InitBinder標(biāo)簽
在我的項(xiàng)目中是在BaseController中增加方法initBinder,并使用注解@InitBinder標(biāo)注,那么spring mvc在綁定表單之前,都會(huì)先注冊(cè)這些編輯器,當(dāng)然你如果不嫌麻煩,你也可以單獨(dú)的寫在你的每一個(gè)controller中。剩下的控制器都繼承該類。spring自己提供了大量的實(shí)現(xiàn)類,諸如CustomDateEditor ,CustomBooleanEditor,CustomNumberEditor等許多,基本上夠用。
當(dāng)然,我們也可以不使用他自己自帶這些編輯器類,那下面我們自己去構(gòu)造幾個(gè)
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也可以。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何使用IDEA 搭建 SpringCloud 項(xiàng)目
所謂微服務(wù),就是要把整個(gè)業(yè)務(wù)模塊拆分成多個(gè)各司其職的小模塊,做到單一職責(zé)原則,不會(huì)重復(fù)開發(fā)相同的業(yè)務(wù)代碼,實(shí)現(xiàn)真正意義上的高內(nèi)聚、低耦合,這篇文章主要介紹了如何使用IDEA 搭建 SpringCloud 項(xiàng)目,需要的朋友可以參考下2023-11-11
Hibernate用ThreadLocal模式(線程局部變量模式)管理Session
今天小編就為大家分享一篇關(guān)于Hibernate用ThreadLocal模式(線程局部變量模式)管理Session,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03
java遞歸實(shí)現(xiàn)樹形結(jié)構(gòu)數(shù)據(jù)完整案例
遞歸算法的代碼比較簡(jiǎn)潔,可讀性較好;但是在實(shí)際的業(yè)務(wù)處理中會(huì)出現(xiàn)多次的重復(fù)調(diào)用,如果處理不好,很容易出現(xiàn)StackOverflowError報(bào)錯(cuò),這篇文章主要給大家介紹了關(guān)于java遞歸實(shí)現(xiàn)樹形結(jié)構(gòu)數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2023-04-04
IDEA 使用 SpotBugs 找出你代碼中的bug問(wèn)題
這篇文章主要介紹了IDEA 使用 SpotBugs 找出你代碼中的bug問(wèn)題,重點(diǎn)給大家介紹SpotBugs 在 idea 中的安裝和使用,感興趣的朋友跟隨小編一起看看吧2021-07-07

