SpringMVC中利用@InitBinder來(lái)對(duì)頁(yè)面數(shù)據(jù)進(jìn)行解析綁定的方法
在使用SpingMVC框架的項(xiàng)目中,經(jīng)常會(huì)遇到頁(yè)面某些數(shù)據(jù)類(lèi)型是Date、Integer、Double等的數(shù)據(jù)要綁定到控制器的實(shí)體,或者控制器需要接受這些數(shù)據(jù),如果這類(lèi)數(shù)據(jù)類(lèi)型不做處理的話將無(wú)法綁定。
這里我們可以使用注解@InitBinder來(lái)解決這些問(wèn)題,這樣SpingMVC在綁定表單之前,都會(huì)先注冊(cè)這些編輯器。一般會(huì)將這些方法些在BaseController中,需要進(jìn)行這類(lèi)轉(zhuǎn)換的控制器只需繼承BaseController即可。其實(shí)Spring提供了很多的實(shí)現(xiàn)類(lèi),如CustomDateEditor、CustomBooleanEditor、CustomNumberEditor等,基本上是夠用的。
demo如下:
public class BaseController { @InitBinder protected void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Date.class, new MyDateEditor()); binder.registerCustomEditor(Double.class, new DoubleEditor()); binder.registerCustomEditor(Integer.class, new IntegerEditor()); } private class MyDateEditor extends PropertyEditorSupport { @Override public void setAsText(String text) throws IllegalArgumentException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = null; try { date = format.parse(text); } catch (ParseException e) { format = new SimpleDateFormat("yyyy-MM-dd"); try { date = format.parse(text); } catch (ParseException e1) { } } setValue(date); } } 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(); } } 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(); } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決idea默認(rèn)帶的equals和hashcode引起的bug
這篇文章主要介紹了解決idea默認(rèn)帶的equals和hashcode引起的bug,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07在SpringBoot項(xiàng)目中使用Spring Cloud Sentinel實(shí)現(xiàn)流量控制
隨著微服務(wù)架構(gòu)的流行,服務(wù)之間的調(diào)用變得越來(lái)越頻繁和復(fù)雜,流量控制是保障系統(tǒng)穩(wěn)定性的重要手段之一,它可以幫助我們避免因過(guò)載而導(dǎo)致的服務(wù)不可用,本文將介紹如何在Spring Boot項(xiàng)目中使用Spring Cloud Sentinel來(lái)實(shí)現(xiàn)流量控制,需要的朋友可以參考下2024-08-08Java8實(shí)戰(zhàn)之Stream的延遲計(jì)算
JDK中Stream的中間函數(shù)如 filter(Predicate super T>)是惰性求值的,filter并非對(duì)流中所有元素調(diào)用傳遞給它的Predicate,下面這篇文章主要給大家介紹了關(guān)于Java8實(shí)戰(zhàn)之Stream延遲計(jì)算的相關(guān)資料,需要的朋友可以參考下2021-09-09Java圖片處理開(kāi)源框架Thumbnailator
這篇文章主要為大家詳細(xì)介紹了Java圖片處理開(kāi)源框架Thumbnailator的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05DynamicDataSource怎樣解決多數(shù)據(jù)源的事務(wù)問(wèn)題
這篇文章主要介紹了DynamicDataSource怎樣解決多數(shù)據(jù)源的事務(wù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07通過(guò)System.getProperty配置JVM系統(tǒng)屬性
這篇文章主要介紹了通過(guò)System.getProperty配置JVM系統(tǒng)屬性,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10