zoukankan      html  css  js  c++  java
  • Spring表单的initBinder:绑定表单复杂属性

    initBinderprotected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
    Initialize the given binder instance, for example with custom editors. Called by createBinder.

    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:
    request - current HTTP request
    binder - new binder instance
    ----------------------------------------------笨拙的分割线----------------------------------------
    回想以前学习IoC容器的时候,有提到“属性编辑器”,只要在IoC配置文件里注册特定“编辑器”,就可以将String转换成javabean。
    翻了翻书,想要自定义属性编辑器,只要继承PropertyEditorSupport,并重写里面的setAsText方法,再进行注册就行了。只不过 书上是在IoC容器的配置文件注册,而这里恐怕是通过重写initBinder方法注册。
    initBinder有一个入参binder就是用来注册属性编辑器的,它是ServletRequestDataBinder类型,查看API,有一个 来自父类DataBinder的方法——registerCustomEditor:
    public void registerCustomEditor(Class requiredType, String field, PropertyEditor propertyEditor)
    Description copied from interface: PropertyEditorRegistry
    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容器里该注入的要注入。 

  • 相关阅读:
    2016/3/16 高级查询 ①连接查询 ②联合查询 ③子查询 无关 相关
    2016/3/13 七种查询 (普通查询 条件查询 排序查询 模糊查询 统计查询 分组查询 分页查询 )
    2016/3/13 MySQL 增删查改 CRUD 用代码实现
    2016/3/10 数据库简单操作( 创建数据库 创建表 数值类型 主键 外键 自动递增 )
    2016/3/10 PHP环境搭建 LAMP WAMP
    2016/3/10 PHP (超文本预处理器) 是什么?
    2016/3/1 淘宝 腾讯 网易 css初始化代码 以及最基础的初始化
    判断i在字符串中出现的次数(2016.1.12P141-1)
    2016-1-9作业——输出二维数组的和
    2016-1-8作业
  • 原文地址:https://www.cnblogs.com/danghuijian/p/4400082.html
Copyright © 2011-2022 走看看