Validation passed in SpringMVC3.0.5.
Spring MVC has a common set of property editors, including the basic data types and their wrapper class property editors, the String property Editor, the JavaBean property editor, and so on. But we also need to register some of our own custom property editors, such as the property editor for a particular time format, is a typical example.
Spring MVC allows the property editor to be registered to the entire spring framework, which works for all controllers. (Configured via Annotationmethodhandleradapter)
SPRINGMVC can also register a separate property editor for only one controller, at which point the Property editor acts only on the current controller. (via @initbinder annotations)
1. Register the custom editor with the entire Spring MVC framework: In the Dispatcher-servlet.xml file, configure the following:
<!--start the annotation-based SPRINGMVC. Note: When <mvc:annotation-driven/> is used, it Defaultannotationhandlermapping and Annotationmethodhandleradapter two beans are automatically registered. The Global property editor cannot be specified at this time, and the solution is to manually add the above bean. -
<!--<mvc:annotation-driven/>--
<bean class= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<property name= "Webbindinginitializer" >
<bean class= "Com.springmvc.demo.util.DateBindingEditor"/>
</property>
</bean>
|
Package com.springmvc.demo.util;
Import Java.util.Date;
Import Java.text.SimpleDateFormat;
Import Org.springframework.beans.propertyeditors.CustomDateEditor;
Import Org.springframework.web.bind.WebDataBinder;
Import Org.springframework.web.bind.support.WebBindingInitializer;
Import Org.springframework.web.context.request.WebRequest;
public class Datebindingeditor implements Webbindinginitializer {
@Override
public void Initbinder (Webdatabinder binder, WebRequest arg1) {
TODO auto-generated Method Stub
System.out.println (This.getclass () + "---initbinder---");
New Customdateeditor (New SimpleDateFormat ("Yyyy-mm-dd"), true);
}
}
|
2. Define a separate property editor in controller
@Controller @RequestMapping (value= "/test1") @SessionAttributes ("Curruser") public class Mycontroller { ...... @InitBinder public void Initbinder (Servletrequestdatabinder binder) { Binder.registercustomeditor (Date.class, New Customdateeditor (New SimpleDateFormat ("Yyyy-mm-dd"), true); } ...... } |
Springmvc the custom Property editor