2010-08-13 3 views
8

유형은 개체의 enum 속성입니다.ENUM을 라디오 버튼에 바인딩하는 방법은 무엇입니까?

<form:radiobutton path="type" value="Male" /> 

자바 :

public enum TestType 
{ 
    Male, Female; 
} 

및 오류

입력 유형에서 'java.lang.String의를'값 '남성'을 변환 할 수 없습니다 '자바를 가지고

JSP

.lang.Enum '; 이유 = Enum 대신 TestType로 decalred되는 커맨드 객체의 속성 type 아마도

+0

내 잘못입니다. 유형이 _Enum이었습니다 type_ _TestType type_ (으)로 변경되었으며 정상입니다! – Alexander

답변

0

'java.lang.Enum는 열거 형 아니다'?

4

public enum TestType { 

    MAN("Man"), 
    FEMALE("Female"); 

    private String description; 

    private TestType(String description) { 
     this.description = description; 
    } 

    public String getValue() { 
     return name(); 
    } 

    public void setValue(String value) {} 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

} 

을 다음과

model.addAttribute(TestType.values()); 
을 다음과 같이 귀하는 TestType을 설정 그리고

dataBinder.registerCustomEditor(TestType.class, new PropertyEditorSupport() { 
     @Override 
     public void setAsText(String value) throws IllegalArgumentException { 
      if(StringUtils.isBlank(value)) 
       return; 

      setValue(TestType.valueOf(value)); 
     } 

     @Override 
     public String getAsText() { 
      if(getValue() == null) 
       return ""; 

      return ((TestType) getValue()).name(); 
     } 
    }); 

<form:radiobuttons path="type" items="${testTypeList}" itemLabel="description"/> 

다음과 같이 정의 바인더를 등록으로

+1

이 솔루션은 한 번에 나를 위해 일했습니다 ... 봄 3.0 만 변경하고

harshit

+0

@harshit에서 itemLabel 속성을 제거하지만 어떻게 itemlabel을 지정하면 위의 방법이 저를 위해 작동하지 않습니다 ... ? – NimChimpsky

7

더 간단한 해결책은 spring forum에서 찾을 수 있습니다. 맞춤 바인딩이 필요하지 않습니다.

+1

이 대신 허용됩니다. – arthur

관련 문제