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

posted @ 2013-07-12 10:53  黑格  阅读(377)  评论(0编辑  收藏  举报