2012-09-04 5 views
2

사용자 정의 등록 정보 편집기를 등록하는 데 문제가 있습니다. 나는 이것을 다음과 같이 등록한다 :많은 등록 정보 편집자 등록

class BooleanEditorRegistrar implements PropertyEditorRegistrar { 

    public void registerCustomEditors(PropertyEditorRegistry registry) { 
     registry.registerCustomEditor(Boolean.class, 
     new CustomBooleanEditor(CustomBooleanEditor.VALUE_YES, CustomBooleanEditor.VALUE_NO, false)) 
     registry.registerCustomEditor(Boolean.class, 
     new CustomBooleanEditor(CustomBooleanEditor.VALUE_ON, CustomBooleanEditor.VALUE_OFF, true)) 
    } 
} 

그러나 처음으로 적용된다. 더 많은 것을 등록 할 수 있습니까?

답변

2

클래스 당 하나의 속성 편집기 만 설정할 수 있습니다. Spring의 CustomBooleanEditor을 사용한다면, 하나의 값으로 기본값 ("true"/ "on"/ "yes"/ "1", "false"/ "off"/ "no"/ "0" -arg 생성자 또는 true 및 false 각각에 대해 정확히 하나의 문자열. 보다 융통성있는 것을 필요로한다면, 자신 만의 속성 편집기를 구현해야 할 것이다. 예 :

import org.springframework.beans.propertyeditors.CustomBooleanEditor 

class MyBooleanEditor extends CustomBooleanEditor { 

    def strings = [ 
     (VALUE_YES): true, 
     (VALUE_ON): true, 
     (VALUE_NO): false, 
     (VALUE_OFF): false 
    ] 

    MyBooleanEditor() { 
     super(false) 
    } 

    void setAsText(String text) { 
     def val = strings[text.toLowerCase()] 
     if (val != null) { 
      setValue(val) 
     } else { 
      throw new IllegalArgumentException("Invalid boolean value [" + text + "]") 
     } 
    } 
} 
+0

작동합니다. 감사합니다. –