2009-08-12 10 views
22

używam następujące niestandardowe edytor w wielu kontrolerów Wiosna-MVC według:Jak mogę zarejestrować globalny edytor niestandardowy w Spring-MVC?

kontrolera

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

inny kontroler

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

innego kontrolera

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

zauważenie ten sam niestandardowy edytor zarejestrowany

Pytanie: Jak skonfigurować globalny edytor niestandardowy, taki jak ten, aby uniknąć konfigurowania każdego kontrolera?

pozdrowienia,

Odpowiedz

13

Musisz zadeklarować go w kontekście aplikacji:

<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> 

Szczegóły są here

+0

Does zastępuje domyślne Spring PropertyEditors? –

+0

Tak. Strona, do której nawiązałem, wyraźnie stwierdza, że ​​(Tabela 5.2 Wbudowane PropertyEditors) – ChssPly76

+4

Właściwość customEditors jest przestarzała i zostanie usunięta wiosną 3 (zgodnie z javadoc). Zamiast tego należy użyć właściwości PropertyEditorRegistrars. – skaffman

12

Jeśli używasz kontrolera na podstawie adnotacji (sprężyna 2.5+), można użyć a WebBindingInitializer do rejestracji edytorów właściwości globalnych. Coś jak

public class GlobalBindingInitializer implements WebBindingInitializer { 

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

} 

Więc w pliku kontekstowego aplikacji internetowych, oświadczam

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

ten sposób cały regulator adnotacji oparty można użyć dowolnego edytora właściwości zadeklarowane w GlobalBindingInitializer.

30

Począwszy od wersji Spring 3.2, można użyć @ControllerAdvice zamiast używać @ExceptionHandler, @InitBinder i @ModelAttribute w każdym kontrolerze. Zostaną one zastosowane do wszystkich komponentów 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)); 
    } 
} 

Gdybyś zaczął się z wiosennym Roo wygenerowany kod, lub ograniczyć adnotacje zeskanowane przez Component-skanować za pomocą zawierać filtr, następnie dodać wymaganą filtr w 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> 
Powiązane problemy