注册许多房产编辑

时间:2012-09-04 14:49:05

标签: grails

我在注册自定义属性编辑器时遇到问题。我这样注册:

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

但唯一的第一个是应用。是否可以注册多个?

1 个答案:

答案 0 :(得分:2)

每个类只能设置一个属性编辑器。如果你使用的是Spring CustomBooleanEditor,你可以使用默认值(“true”/“on”/“yes”/“1”,“false”/“off”/“no”/“0” )使用one-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 + "]")
        }
    }
}