• Spring MVC中数据绑定(转)


    Spring MVC中数据绑定

    比如Product有一个createTime属性,是java.util.Date类型。那么最简单的转型处理是,在SimpleFormController中覆盖initBinder方法。如:

    @Override protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { binder.registerCustomEditor(Date.class, new CustomDateEditor( new SimpleDateFormat("yyyy-MM-dd"), true)); }

    复用这部分代码的办法,可以编写自己的SimpleFormController子类,再以此为父类,所有表单的Controller通过继承复用。

    或者,编写一个WebBindingInitializer的子类,比如:

    public class MyBindingInitializer implements WebBindingInitializer { @Override public void initBinder(WebDataBinder binder, WebRequest request) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor( dateFormat, true)); } }

    然后配置到比如SimpleFormController中:

    <bean name="/save.htm" class="product.SaveProductController"> <property name="formView" value="input" /> <property name="successView" value="redirect:/browse.htm" /> <property name="commandClass" value="product.Product" /> <property name="productDao" ref="productDao" /> <property name="webBindingInitializer"> <bean class="product.MyBindingInitializer"/> </property>

    <bean/>

    转自:http://marshal.easymorse.com/archives/1243

  • 相关阅读:
    redis的事务不是原子性
    Jenkins持续集成 入门实践
    Docker入门实践
    程序员该有的职业素养
    ubuntu sudoers配置错误
    日志分析工具 Log Parser
    压力测试记录
    Winscp使用sudo user登录
    Linux下通过NFS共享文件夹
    Jexus 5.4.6 on CentOS 6.6
  • 原文地址:https://www.cnblogs.com/javafan/p/3185855.html
Copyright © 2020-2023  润新知