日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring表单的initBinder:绑定表单复杂属性

發布時間:2025/1/21 javascript 55 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring表单的initBinder:绑定表单复杂属性 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天碰到一個問題,頁面表單上是一個id,但在表單控制器的command里是一個javabean,如果將一個String轉換成javabean呢?因為已經有了一個服務于hibernate的javabean,我可不想再寫一個javabean,然后再笨笨的轉換。

在查看SimpleFormController的API的時候,發現它有一個來自父類BaseCommandController的方法——initBinder:
BaseCommandController (Spring Framework)

initBinder

protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
Initialize the given binder instance, for example with custom editors. Called by?de<createBinderde<.

This method allows you to register custom editors for certain fields of your command class. For instance, you will be able to transform Date objects into a String pattern and back, in order to allow your JavaBeans to have Date properties and still be able to set and display them in an HTML interface.

Default implementation is empty.

Parameters:
de<requestde< - current HTTP request
de<binderde< - new binder instance
----------------------------------------------笨拙的分割線----------------------------------------
回想以前學習IoC容器的時候,有提到“屬性編輯器”,只要在IoC配置文件里注冊特定“編輯器”,就可以將String轉換成javabean。
翻了翻書,想要自定義屬性編輯器,只要繼承PropertyEditorSupport,并重寫里面的setAsText方法,再進行注冊就行了。只不過書上是在IoC容器的配置文件注冊,而這里恐怕是通過重寫initBinder方法注冊。
initBinder有一個入參binder就是用來注冊屬性編輯器的,它是ServletRequestDataBinder類型,查看API,有一個來自父類DataBinder的方法——registerCustomEditor:
DataBinder (Spring Framework)public void registerCustomEditor(Class requiredType, String field, PropertyEditor propertyEditor)
Description copied from interface:?de<PropertyEditorRegistryde<
Register the given custom property editor for the given type and property, or for all properties of the given type.

If the property path denotes an array or Collection property, the editor will get applied either to the array/Collection itself (the PropertyEditor has to create an array or Collection value) or to each element (the PropertyEditor has to create the element type), depending on the specified required type.

Note: Only one single registered custom editor per property path is supported. In case of a Collection/array, do not register an editor for both the Collection/array and each element on the same property.

------------------------------------------愚蠢的分割線-------------------------------- 方法的入參名已經很明顯地暴露了意圖。requiredType顯然是指command里的javabean,field顯然是指在command里對應的字段名,同時也是表單里對應的name,而propertyEditor就是自定義的屬性編輯器。例子:
//自定義屬性編輯器
public class CollegeEditor extends PropertyEditorSupport{
??? private CollegeService collegeService;

??? public CollegeService getCollegeService() {
??? ??? return collegeService;
??? }

??? public void setCollegeService(CollegeService collegeService) {
??? ??? this.collegeService = collegeService;
??? }
????
??? public void setAsText(String collegeId){
??? ??? int id = Integer.valueOf(collegeId);
??? ??? College college = collegeService.findCollegeById(id);
??? ??? setValue(college);
??? }
}
//重寫SimpleFormController的initBinder方法
public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder){
??? ??? binder.registerCustomEditor(College.class, "college", collegeEditor);
??? }

當然不要忘記IoC容器里該注入的要注入。

總結

以上是生活随笔為你收集整理的Spring表单的initBinder:绑定表单复杂属性的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。