Spring中日期类型的绑定

来源:互联网 发布:网卡mac地址哪个是 编辑:程序博客网 时间:2024/06/03 23:16
首先在action-servlet.xml中配置如下:
    
<!-- property editor-->         
   <beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
      <propertyname="webBindingInitializer">
          <beanclass="com.morris.MyBindingInitializer"/>
      </property>
   </bean>
Java代码:
package com.spring.demo;


public class MyBindingInitializer implements WebBindingInitializer{

    public voidinitBinder(WebDataBinder binder, WebRequest request) {
       SimpleDateFormat dateFormat = newSimpleDateFormat("yyyy-MM-dd"); //可以设定任意的日期格式
       dateFormat.setLenient(false);
       binder.registerCustomEditor(Date.class,
           new CustomDateEditor(dateFormat, true));
       binder.registerCustomEditor(String.class, newStringTrimmerEditor(false));
    }


}
0 0
原创粉丝点击