2011-08-10 1 views
1

데이터 바인딩 초기화에는 두 가지 주요 방법이 있지만 올드 스쿨의 단점이 있습니다. 알아낼 수없는 단점이 있습니다. 이 특수 효과 방법은 훌륭합니다.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 클래스뿐만 아니라 overwrited getAsText를, getValue, setValue의 및 setAsText는 방법과 java.beans.PropertyEditorSupport에의 서브 클래스가
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception 
{ 
    System.out.println("Binding!!!!!"); 
    super.initBinder(request, binder); 
    binder.registerCustomEditor(Double.class, new CurrencyPropertyEditor()); 
} 

.

희망이 있습니다. !!!