質問

There are two major means of data binding initialization, but there is a drawback in the oldschool one, that I can't figure out. This annotation way is great :

@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(","));
}

but I can't be done with ConfigurableWebBindingInitializer. First off, the binder instance is created in AnnotationMethodHandlerAdapter and initializer is passed the binder instance somewhere in HandlerMethodInvoker so I can't set it up... I can't do something like this :

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

Because binder instance is passed into it in handlerAdapter. How can I set up the binder then ?

役に立ちましたか?

解決

There is no way of setting it up in xml configuration. You must implement your custom WebBindingInitializer ... The ConfigurableWebBindingInitializer is obviously missing the possibility of setting up allowed and restricted fields...

Or you can vote up SPR-8601

他のヒント

This is very old, however for anyone that dislike the use of annotations in production code (like me) here is a solution I found to add a init binder without use of annotations. You only need to overwrite initBinder method that extends from most of base controllers provided by Spring:

protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
{
    System.out.println("Binding!!!!!");
    super.initBinder(request, binder);
    binder.registerCustomEditor(Double.class, new CurrencyPropertyEditor());
}

Where my CurrencyPropertyEditor class is a subclass of java.beans.PropertyEditorSupport with getAsText, getValue, setValue and setAsText methods overwrited as well.

Hope it helps!!!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top