2012-01-04 3 views
0

비슷한 여러 질문을 본 적이 있지만 아직이 문제를 해결하는 대답을 찾지 못했습니다. 만큼, 이제스프링 MVC - 빈 폼 값을 기본 빈 필드에 바인딩

<form:form action="count.do"> 
    <form:input id="counter" path="counter"/> 
</form:form> 

: 나는 (명확성을 위해 약칭 함) 한

public class MyBean { 
    private int counter; 

    public int getCounter() { 
    return counter; 
    } 

    public void setCounter(int counter) { 
    this.counter = counter; 
    } 
} 

는 JSP 형태 : 나는 원시 속성 명령 객체에 의해 뒷받침되는 스프링 MVC 양식을 가지고 거기에 "카운터"입력 필드에 유효한 숫자 값은 모든 것이 예상 작품으로,하지만 필드가 비어 있다면, 나는 양식 유효성 검사 오류 얻을 : 나는 사용자 지정 속성 편집기를 만드는 시도했습니다

org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'MyBean' on field 'counter': rejected value []; codes [methodInvocation.MyBean.counter,methodInvocation.counter,methodInvocation.float,methodInvocation]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [MyBean.counter,counter]; arguments []; default message [counter]]; default message [Property 'counter' threw exception; nested exception is java.lang.IllegalArgumentException] 

을 에 널 (null) 값을 허용 (initBinder()에서 초기화),하지만 난 래퍼 접근의 비 기본 세트 만들 수있을 경우에만 작동합니다 그래서

binder.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, true)); 

을하는 폼에 필드를 결합하는 방법은 무엇입니까 프리미티브가 아닌 프리미티브 래퍼를 만들지 않고 여전히 공백 값을 처리합니다 (오류가 아닌 0으로 설정하고 싶습니다). 그렇다면 간단한 예제를 게시 하시겠습니까?

많은 감사, 피터

답변

0

나는 그것을 tryed 없지만 int.class에 대한 편집기 (이 정말 작품)를 등록하려고 않습니다.

하지만 intnullnew CustomNumberEditor(Integer.class, true)을 처리 할 수 ​​없기 때문에 작동하지 않습니다.

예를 들어 0을 반환하는 숫자 편집기가 필요합니다. 값이 정의되어 있지 않습니다.

binder.registerCustomEditor(int.class, 
    new MyNumberEditorThatConvertsNullToZero());