2011-03-10 11 views
6

Me preguntaba si había una forma de registrar un PropertyEditor globalmente dentro de Spring MVC 3.0 en adelante. En la documentación their documentation, muestran cómo usar las anotaciones para personalizar Bean PropertyEditor según cada controlador, y -me parece a mí- como una forma XML de hacerlo globalmente. Así que me preguntaba, ¿hay alguna manera, usando solo anotaciones para registrar PropertyEditors para todos los controladores sin tener que hacer un método @InitBinder para cada uno? Tampoco es deseable crear una superclase común con un método @InitBinder.¿Existe alguna manera anotada de registrar PropertyEditors globalmente en Spring MVC 3.0?

Se solicitó el other question sobre este tema antes de que se lanzara Spring 3.0.

Respuesta

5
package com.projectr.web; 

import java.text.SimpleDateFormat; 
import java.util.Date; 

import org.springframework.beans.propertyeditors.CustomBooleanEditor; 
import org.springframework.beans.propertyeditors.CustomDateEditor; 
import org.springframework.beans.propertyeditors.CustomNumberEditor; 
import org.springframework.web.bind.WebDataBinder; 
import org.springframework.web.bind.support.WebBindingInitializer; 
import org.springframework.web.context.request.WebRequest; 
import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor; 

/** 
* Shared WebBindingInitializer for custom property editors. 
* 
* @author aramirez 
* 
*/ 
public class CommonBindingInitializer implements WebBindingInitializer { 
    public void initBinder(WebDataBinder binder, WebRequest request) { 
     binder.registerCustomEditor(Integer.class, null, new CustomNumberEditor(Integer.class, null, true)); 
     binder.registerCustomEditor(Long.class, null, new CustomNumberEditor(Long.class, null, true)); 
     binder.registerCustomEditor(Boolean.class, null, new CustomBooleanEditor(true)); 
     binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor()); 
     SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy", request.getLocale()); 
     dateFormat.setLenient(false); 
     binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, true)); 
    } 
} 

En el contexto de aplicación o expedidor en servlet

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> 
    <property name="webBindingInitializer"> 
     <bean class="com.projectr.web.CommonBindingInitializer"/> 
    </property> 
    </bean> 

anotación equivalente del código de seguridad. Tenga en cuenta que @ControllerAdvice se introduce en la primavera 3.2.x

@ControllerAdvice 
public class CommonBindingInitializer { 
    @InitBinder 
    public void registerCustomEditors(WebDataBinder binder, WebRequest request) { 
    binder.registerCustomEditor(Integer.class, null, new CustomNumberEditor(Integer.class, null, true)); 
    binder.registerCustomEditor(Long.class, null, new CustomNumberEditor(Long.class, null, true)); 
    binder.registerCustomEditor(Boolean.class, null, new CustomBooleanEditor(true)); 
    binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor()); 
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy", request.getLocale()); 
    dateFormat.setLenient(false); 
    binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, true)); 
    } 
} 
+0

Teniendo en cuenta que el ejemplo no utiliza anotaciones _at ALL_ No veo cómo esto es una respuesta a la pregunta. – ArtB

+0

Mis disculpas. El equivalente de anotación del código xml anterior es [@ControllerAdvice] (http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/bind/annotation/ControllerAdvice.html) con @InitBinder. Pero supongo que ya te diste cuenta debido a mi respuesta tardía. – ramirezag

+0

No recuerdo si lo hice. ¿Quieres publicar un ejemplo como respuesta adicional y lo aceptaré? \ – ArtB

Cuestiones relacionadas