2009-08-12 15 views
22

Io uso il seguente editor personalizzato nei controllori MOLTI primavera-MVC in base a:Come posso registrare un editor personalizzato globale in Spring-MVC?

Un controller

binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, NumberFormat.getNumberInstance(new Locale("pt", "BR"), true)); 

altro controller

binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, NumberFormat.getNumberInstance(new Locale("pt", "BR"), true)); 

un altro controller

binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, NumberFormat.getNumberInstance(new Locale("pt", "BR"), true)); 

Avviso del stesso editor personalizzato registrato

Domanda: come posso impostare un editor personalizzato globale come questo al fine di evitare l'impostazione di ciascun controller?

saluti,

risposta

13

è necessario dichiarare nel proprio contesto di applicazione:

<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> 
    <property name="customEditors"><map> 
    <entry key="java.math.BigDecimal"> 
     <bean class="org.springframework.beans.propertyeditors.CustomNumberEditor"> 
     ... <!-- specify constructor-args here --> 
     </bean> 
    </entry> 
    </map></property> 
</bean> 

dettagli sono here

+0

fa sovrascrive Spring PropertyEditors di default? –

+0

Sì. La pagina I collegata sopra specifica specificamente (Tabella 5.2. PropertyEditors incorporati) – ChssPly76

+4

La proprietà CustomEditors è obsoleta e verrà rimossa in Spring 3 (in base a javadoc). Dovresti invece utilizzare la proprietà PropertyEditorRegistrars. – skaffman

12

Se si utilizza un controller di annotazione basato (primavera 2.5+), è possibile utilizzare un oggetto WebBindingInitializer per registrare editor di proprietà globali. Qualcosa di simile

public class GlobalBindingInitializer implements WebBindingInitializer { 

    public void initBinder(WebDataBinder binder, WebRequest request) { 
     binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), true)); 
    } 

} 

Quindi nel tuo file di contesto applicazione web, dichiarare

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
    <property name="webBindingInitializer"> 
     <bean class="GlobalBindingInitializer"/> 
    </property> 
</bean> 

In questo modo tutti i controller di annotazione basato può utilizzare qualsiasi editor di proprietà dichiarata in GlobalBindingInitializer.

30

Starting Spring 3.2, è possibile utilizzare @ControllerAdvice anziché utilizzare @ExceptionHandler, @InitBinder e @ModelAttribute in ciascun controller. Saranno applicati a tutti i bean @Controller.

import org.springframework.web.bind.WebDataBinder; 
import org.springframework.web.bind.annotation.ControllerAdvice; 
import org.springframework.web.bind.annotation.InitBinder; 
import org.springframework.web.context.request.WebRequest; 

@ControllerAdvice 
public class GlobalBindingInitializer { 
    @InitBinder 
    public void registerCustomEditors(WebDataBinder binder, WebRequest request) { 
    binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, NumberFormat.getNumberInstance(new Locale("pt", "BR"), true)); 
    } 
} 

Se tu avessi iniziato con la Primavera Roo Codice generato, o limitare le annotazioni scanditi dal componente-scan utilizzando include-filtro, quindi aggiungere il filtro richiesto in webmvc-config.xml

<!-- The controllers are autodetected POJOs labeled with the @Controller annotation. --> 
<context:component-scan base-package="com.sensei.encore.maininterface" use-default-filters="false"> 
    <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/> 
    <!-- ADD THE BELOW LINE --> 
    <context:include-filter expression="org.springframework.web.bind.annotation.ControllerAdvice" type="annotation"/> 
</context:component-scan> 
Problemi correlati