2009-08-12 15 views
22

私はに従ってMANY春MVCコントローラで次のカスタム・エディタを使用します。Spring MVCでグローバルカスタムエディタを登録するにはどうしたらいいですか?

Aコントローラ

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

他のコントローラ

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

別のコントローラ

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

同じことに注意してくださいカスタムエディタを登録しました

質問:どのように各コントローラの設定を避けるために、このようなグローバルカスタムエディタを設定できますか?

について、

答えて

13

あなたはあなたのアプリケーションコンテキストでそれを宣言する必要があります:

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

詳細は、アノテーションベースのコントローラ(春2.5+)を使用する場合は、使用することができますhere

+0

んデフォルトのSpring PropertyEditorsをオーバーライドしますか? –

+0

はい。上記のリンク先のページには、(Table 5.2。Built-in PropertyEditors) – ChssPly76

+4

があります。customEditorsプロパティは廃止され、Spring 3で削除されます(javadocによると)。代わりにPropertyEditorRegistrarsプロパティを使用する必要があります。 – skaffman

12

ですグローバルプロパティエディタを登録するWebBindingInitializer。

public class GlobalBindingInitializer implements WebBindingInitializer { 

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

} 

のようなものは、だからあなたのWebアプリケーションのコンテキスト・ファイルで、

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

すべてのアノテーションベースのコントローラは、任意のプロパティエディタを使用することができます。この方法はGlobalBindingInitializerで宣言された宣言します。

30

Spring 3.2から、各コントローラで@ExceptionHandler、@InitBinder、および@ModelAttributeを使用する代わりに、@ControllerAdviceを使用できます。それらはすべての@Controller Beanに適用されます。

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)); 
    } 
} 

あなたは春Rooのコードを生成し始めた、または含まフィルタを使用して、コンポーネント・スキャンによってスキャン注釈を制限していた場合は、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> 
関連する問題