SpringMVC custom attribute editor details and examples
SpringMVC custom attribute editor details and examples
There are two main methods to customize spring MVC Attribute Editor. One is to use the @ InitBinder label to register an Attribute Editor at runtime. This editor is only valid in the current Controller; another method is to implement your own WebBindingInitializer, and then define an AnnotationMethodHandlerAdapter bean to register it in this bean. This Attribute Editor is global.
Method 1:
Import java. beans. propertyEditorSupport; import java. io. IOException; import java. text. simpleDateFormat; import java. util. date; import javax. servlet. http. httpServletResponse; import org. springframework. beans. propertyeditors. customDateEditor; import org. springframework. stereotype. controller; import org. springframework. web. bind. webDataBinder; import org. springframework. web. bind. annotation. initBinder; import org. springframework. web. bind. annotation. pathVariable; import org. springframework. web. bind. annotation. requestMapping; @ Controller public class GlobalController {@ RequestMapping ("test/{date}") public void test (@ PathVariable Date date, HttpServletResponse response) throws IOException response. getWriter (). write (date) ;}@ InitBinder // you must have a parameter WebDataBinder public void initBinder (WebDataBinder binder) {binder. registerCustomEditor (Date. class, new CustomDateEditor (new SimpleDateFormat ("yyyy-MM-dd"), false); binder. registerCustomEditor (int. class, new PropertyEditorSupport () {@ Override public String getAsText () {// TODO Auto-generated method stub return getValue (). toString () ;}@ Override public void setAsText (String text) throws IllegalArgumentException {// TODO Auto-generated method stub System. out. println (text + "................................... ........ "); setValue (Integer. parseInt (text ));}});}}
You can write this method.
Second:
1. define your own WebBindingInitializer
package com.xxx.blog.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 MyWebBindingInitializer implements WebBindingInitializer { @Override public void initBinder(WebDataBinder binder, WebRequest request) { // TODO Auto-generated method stub binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false)); } }
2. Define an AnnotationMethodHandlerAdapter in the springMVC configuration file and set its WebBindingInitializer attribute to our own WebBindingInitializer object.
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="cacheSeconds" value="0"/> <property name="webBindingInitializer"> <bean class="com.xxx.blog.util.MyWebBindingInitializer"/> </property> </bean>
The second method can be used to define a global Attribute Editor after the preceding two steps.
Note: When <mvc: annotation-driven/> is used, it automatically registers two beans: DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter. In this case, the global Attribute Editor specified by the second method does not work. The solution is to manually add the above beans and add them to the front of <mvc: annotation-driven/>.
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!