SpringMVC—数据绑定与属性编辑

来源:互联网 发布:java 连接oracle 编辑:程序博客网 时间:2024/05/01 14:06
数据绑定与属性编辑


当自定义控制器继承的是AbstractCommandController控制器,那么可以重写initBinder方法:
下面的方法将页面中的参数转换成date类型,下面使用的属性编辑器是由SpringMVC提供的,也可以自己定义一个属性编辑器可以继承PropertyEditorSupport类。
@Override
protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception {

  //registerCustomEditor方法的第一个参数为要转换类型的class,第二个参数为属性编辑器
  //CustomDateEditor属性编辑器类的构造方法的第一个参数为格式,第二个参数为是否可以为空
  binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));


}
原创粉丝点击