深入解析@InitBinder注解的功能與應(yīng)用
一、注解作用
從字面意思可以看出這個的作用是給Binder做初始化的,被此注解的方法可以對WebDataBinder初始化。webDataBinder是用于表單到方法的數(shù)據(jù)綁定的! @InitBinder只在@Controller中注解方法來為這個控制器注冊一個綁定器初始化方法,方法只對本控制器有效。
二、代碼演示
1.對數(shù)據(jù)綁定進(jìn)行設(shè)置
WebDataBinder中有很多方法可以對數(shù)據(jù)綁定進(jìn)行具體的設(shè)置:比如我們設(shè)置name屬性為非綁定屬性(也可以設(shè)置綁定值setAllowedFields):
在Controller中添加一個方法:
@InitBinder public void initBinder(WebDataBinder binder) { binder.setDisallowedFields("name"); }
然后運(yùn)行:
添加:
看后面那個name值就沒有綁定成功!
(具體作用視頻舉了個例子來闡述其某個作用:比如表單中有多選按鈕,對應(yīng)的是JaveBean中的一個集合屬性,然而選擇的是id,而集合保存的確實(shí)類,這里我們就不能任SpringMVC自動綁定,需要我們手動綁定,所以就關(guān)閉其的自動綁定,不然會報錯?。?/p>
2.注冊已有的編輯器
WebDataBinder是用來綁定請求參數(shù)到指定的屬性編輯器.由于前臺傳到controller里的值是String類型的,當(dāng)往Model里Set這個值的時候,如果set的這個屬性是個對象,Spring就會去找到對應(yīng)的editor進(jìn)行轉(zhuǎn)換,然后再set進(jìn)去!Spring自己提供了大量的實(shí)現(xiàn)類(如下圖所示的在org.springframwork.beans.propertyEditors下的所有editor),諸如CustomDateEditor ,CustomBooleanEditor,CustomNumberEditor等許多,基本上夠用。 在平時使用SpringMVC時,會碰到j(luò)avabean中有Date類型參數(shù),表單中傳來代表日期的字符串轉(zhuǎn)化為日期類型,SpringMVC默認(rèn)不支持這種類型的轉(zhuǎn)換。我們就需要手動設(shè)置時間格式并在webDateBinder上注冊這個編輯器!
實(shí)現(xiàn)代碼
現(xiàn)在在student類中加上一個類型為Date的birth屬性、setter和getter,新增帶有birth所有屬性的構(gòu)造函數(shù)。(代碼略)
list.jsp(所有學(xué)生信息頁面)加一列顯示生日的:${stu.birth}
input.jsp(添加信息界面):
<br> birth(format=yyyy-mm-dd):<form:input path="birth"/> <br>
然后再控制器中該掉之前的@InitBinder代碼:
@InitBinder public void initBinder(WebDataBinder binder) { //binder.setDisallowedFields("name"); CustomDateEditor editor = new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true); binder.registerCustomEditor(Date.class, editor); } //編輯器構(gòu)造函數(shù) public CustomDateEditor(DateFormat dateFormat, boolean allowEmpty) //注冊編輯器函數(shù) public void registerCustomEditor(Class<?> requiredType, PropertyEditor propertyEditor)
然后運(yùn)行
添加成功
3.注冊自定義編輯器
使用自定義編輯器就是在第二個的基礎(chǔ)上添加個自定義編輯器就行了,自定義的編輯器類需要繼承
- org.springframework.beans.propertyeditors.PropertiesEditor;
并重寫其setAsText和getAsText兩個方法就行了!
比如下面這個DoubleEditor:
public class DoubleEditor extends PropertyEditorSupport { @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(); } }
然后在InitBinder方法中注冊就行。
4.設(shè)置屬性的前綴可以實(shí)現(xiàn)參數(shù)綁定
代碼所示:
jsp: <form action="/testBean" method="post"> name: <input type="text" name="u.name"> <br> age: <input type="text" name="u.age"> <br> name: <input type="text" name="s.name"> <br> age: <input type="text" name="s.age"> <br> <input type="submit"> </form>
controller:
@InitBinder("user") public void init1(WebDataBinder binder) { binder.setFieldDefaultPrefix("u."); } @InitBinder("stu") public void init2(WebDataBinder binder) { binder.setFieldDefaultPrefix("s."); } @RequestMapping("/testBean") public ModelAndView testBean(User user, @ModelAttribute("stu") Student stu) { System.out.println(stu); System.out.println(user); String viewName = "success"; ModelAndView modelAndView = new ModelAndView(viewName); modelAndView.addObject("user", user); modelAndView.addObject("student", stu); return modelAndView; }
@InitBinder(“user”)括號內(nèi)的參數(shù)為類的首字母小寫(默認(rèn)命名規(guī)則),也可以用@ModelAttribute(“stu”)做限定.
到此這篇關(guān)于深入解析@InitBinder注解的功能與應(yīng)用的文章就介紹到這了,更多相關(guān)@InitBinder注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot中讀取jar包中的resources目錄下的文件的三種方式
這篇文章給大家總結(jié)了SpringBoot讀取 jar 包中的 resources 目錄下的文件的三種方式,文中有詳細(xì)的代碼示例供大家參考,,需要的朋友可以參考下2023-06-06spring boot項(xiàng)目application.properties文件存放及使用介紹
這篇文章主要介紹了spring boot項(xiàng)目application.properties文件存放及使用介紹,我們的application.properties文件中會有很多敏感信息,大家在使用過程中要多加小心2021-06-06List集合中對數(shù)據(jù)實(shí)現(xiàn)多重規(guī)則進(jìn)行排序的案例
今天小編就為大家分享一篇關(guān)于List集合中對數(shù)據(jù)實(shí)現(xiàn)多重規(guī)則進(jìn)行排序的案例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12基于Java實(shí)現(xiàn)一個復(fù)雜關(guān)系表達(dá)式過濾器
這篇文章主要為大家詳細(xì)介紹了如何基于Java實(shí)現(xiàn)一個復(fù)雜關(guān)系表達(dá)式過濾器。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-07-07使用Java實(shí)現(xiàn)通用樹形結(jié)構(gòu)構(gòu)建工具類
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)通用樹形結(jié)構(gòu)構(gòu)建工具類,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-03-03