今天碰到一个问题,页面表单上是一个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:绑定表单复杂属性相关推荐

  1. SpringMvc 注解 @InitBinder 表单多对象精准绑定接收

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. @InitBinder用于在@Controller中标注于方法,表示为当前控制器注册一个属性编辑器或 ...

  2. @InitBinder注解绑定表单数据

    在SpringMVC框架的项目中,bean中定义了Date,double等类型,如果没有做任何处理的话,日期以及double都无法绑定.      比如经常会遇到页面表单中某些数据类型是Date.In ...

  3. Spring Security默认的用户登录表单 页面源代码

    Spring Security默认的用户登录表单 页面源代码 <html><head><title>Login Page</title></hea ...

  4. php js获取表单内容,jquery form表单获取内容以及绑定数据_javascript技巧

    在日常开发的过程中,难免会用到form表单,我们需要获取表单的数据保存到数据库,或者拿到后台的一串json数据,要将数据绑定到form表单上,这里我写了一个基于jquery的,formHelp插件,使 ...

  5. vueform表单文件上传_峰哥说技术系列-8.Spring Boot文件上传(Form表单和Ajax方式)

    今日份主题 Spring Boot文件上传(Form表单和Ajax方式) 在Spring Boot中,和文件上传的主要和MultipartResolver接口有关,他有两个实现类 StandardSe ...

  6. Spring MVC和JQuery用于Ajax表单验证

    在本教程中,我们将看到如何使用Ajax和Spring MVC和JQuery在服务器端验证表单. Spring MVC为通过注释驱动的配置采用Ajax提供了非常方便的过程. 我们将使用此注释驱动的配置以 ...

  7. 基于spring自动注入及AOP的表单二次提交验证

    2019独角兽企业重金招聘Python工程师标准>>> 这几天在网上闲逛,看到了几个关于spring的token二次提交问题,受到不少启发,于是自己动手根据自己公司的项目框架结构,制 ...

  8. JS对象迭代、事件处理器、表单控件绑定、表单复选框、表单单选按钮

    JS对象迭代 知识点 v-for v-for 循环JS对象,把对象内容循环显示到页面上. <div id="myApp"><h1>JS对象迭代</h1 ...

  9. jquery form自动绑定表单内容

    $(#表单).serialize()自动绑定

  10. Spring boot + maven + jetty9在提交表单的时候出现Form too large

    使用Spring boot + maven + jetty9 提交表单的时候,表单过长时,会在org.eclipse.jetty.server.Requset抛出Form too large:XXX ...

最新文章

  1. eclipse启动时报错An internal error occurred during: Initializing Java Tooling.
  2. TransDecoder
  3. mysql_connect() 不支持 请检查 mysql 模块是否正确加载
  4. C语言宏定义、宏替换
  5. 理解SQLNET.AUTHENTICATION_SERVICES参数|转|
  6. 有关 AI 人才的 6 个真相
  7. ZZULIOJ 1111: 多个整数的逆序输出(函数专题)
  8. JS Date格式化为yyyy-MM-dd类字符串
  9. Springboot配置fastjson开发
  10. mate10鸿蒙系统,华为将发布 MatePad Pro:搭载鸿蒙系统,麒麟 9000 处理器
  11. 防火墙透明模式下虚拟系统配置实例
  12. 计算机为什么老是重启,为什么电脑开机后总是自动重启?电脑开机后自动重启怎么办?...
  13. 右键后资源管理器重启
  14. php中单选框可以默认选中吗,php selectradio和checkbox默认选择的简单示例
  15. Android仿QQ锁屏状态下消息提醒(震动+提示音)
  16. Filter-url拦截
  17. 2021-2027全球与中国讲解员视频软件市场现状及未来发展趋势
  18. 老虎证券开放api期货合约的创建
  19. 音乐播放器(附源码)
  20. 使用火绒后连接FTP服务器失败

热门文章

  1. C语言 | 使用牛顿法求非线性方程的一个实根(附代码)
  2. php中的全局异常,PHP的全局错误处理详解
  3. JVM实战与原理---内存回收策略
  4. access开发精要(4)-参考与查阅
  5. 【深度学习】NetAug(网络增强)—Dropout的反面
  6. 【深度学习】何恺明经典之作—2009 CVPR Best Paper | Dark Channel Prior
  7. 【数据竞赛】十大重要的时间组合特征!
  8. 【NLP】Pytorch中文语言模型bert预训练代码
  9. 常见算法的python实现(Github标星75.5k+)
  10. 太强了!Scikit-learn 0.22新版本发布,新功能更加方便