2011-08-10 21 views
1

データバインディングの初期化には2つの主要な手段がありますが、私が把握することができない古い学校の欠点があります。この注釈方法は素晴らしいです:WebBindingInitializerへの@InitBinderの初期化の外部化

@InitBinder("order") 
public void initBinder(WebDataBinder binder) { 
    // Problem is that I want to set allowed and restricted fields - can be done here 
    binder.setAllowedFields(allowedFields.split(",")); 
} 

私はConfigurableWebBindingInitializerで行うことはできません。まず第一に、バインダーのインスタンスがAnnotationMethodHandlerAdapterに作成され、初期化子がそのように私はそれを設定することはできません...私はこのような何かを行うことはできませんHandlerMethodInvokerのどこかにバインダーインスタンスを渡されます。

<bean id="codesResolver" class="org.springframework.validation.DefaultMessageCodesResolver" /> 
<bean id="binder" class="org.springframework.web.portlet.bind.PortletRequestDataBinder" scope="prototype"> 
    <property name="allowedFields" value="${allowedFields}" /> 
    <aop:scoped-proxy /> 
</bean> 
<bean id="webBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> 
    <property name="messageCodesResolver" ref="codesResolver" /> 
</bean> 

バインダーインスタンスので、 handlerAdapterでその中に渡されます。バインダーはどうすれば設定できますか?

答えて

2

xml構成で設定する方法はありません。あなたは

... ConfigurableWebBindingInitializerは明らかに許可され、制限されたフィールドを設定する可能性が欠落している...カスタムWebBindingInitializerを実装する必要がありますまたはあなたが嫌い​​な人のためしかし、これは非常に古いですSPR-8601

0

まで投票できます。私のような生産コードでの注釈の使用は、注釈を使用しないでinitバインダーを追加するために見つけた解決策です。あなただけ春が提供するベースコントローラのほとんどから延び方法をinitBinder上書きする必要があります。

私CurrencyPropertyEditorクラスはgetAsTextとjava.beans.PropertyEditorSupportのサブクラスである、のgetValue、setValueのといるsetAsTextメソッドが同様に上書き
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception 
{ 
    System.out.println("Binding!!!!!"); 
    super.initBinder(request, binder); 
    binder.registerCustomEditor(Double.class, new CurrencyPropertyEditor()); 
}