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容器里该注入的要注入。 

  • 相关阅读:
    C#自定义控件的开发:Pin和Connector
    C# 调用第三方DLL完整实例
    利用微软Speech SDK 5.1开发语音识别系统主要步骤
    如何在Android中使用OpenCV
    VS 2012单元测试和测试资源管理器
    .NET Reflector插件FileDisassembler还原源码
    根据powerdesigner的OO模型生成C#代码
    SQLServer2008设置开启INTERNET远程连接
    Kudu-压缩
    kudu的读取数据流程
  • 原文地址:https://www.cnblogs.com/danghuijian/p/4400082.html
Copyright © 2011-2022 走看看