Grails 将请求参数绑定到枚举
我的 Grails 应用程序有大量的枚举,如下所示:
public enum Rating {
BEST("be"), GOOD("go"), AVERAGE("av"), BAD("ba"), WORST("wo")
final String id
private RateType(String id) {
this.id = id
}
static public RateType getEnumFromId(String value) {
values().find {it.id == value }
}
}
如果我有一个如下所示的命令对象:
class MyCommand {
Rating rating
}
我想(例如)自动将值为“wo”的请求参数转换为 Rating.WORST。
此处描述了定义自定义转换器的过程(在将字符串转换为日期的上下文中)。尽管此过程运行良好,但我不想为每个枚举创建一个实现 PropertyEditorSupport 的类。有更好的选择吗?
My Grails application has a large number of enums that look like this:
public enum Rating {
BEST("be"), GOOD("go"), AVERAGE("av"), BAD("ba"), WORST("wo")
final String id
private RateType(String id) {
this.id = id
}
static public RateType getEnumFromId(String value) {
values().find {it.id == value }
}
}
If I have a command object such as this:
class MyCommand {
Rating rating
}
I would like to (for example) automatically convert a request parameter with value "wo" to Rating.WORST.
The procedure for defining custom converters is described here (in the context of converting Strings to Dates). Although this procedure works fine, I don't want to have to create a class implementing PropertyEditorSupport for each of my enums. Is there a better alternative?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了一个令我非常满意的解决方案。
第 1 步: 创建 PropertyEditorSupport 的实现,以将文本与相关 Enum 相互转换
第 2 步: 定义一个类,将 EnumEditor 注册为各种枚举类的转换器。要更改可通过 id 绑定的枚举类列表,只需修改
BINDABLE_ENUMS
第 3 步: 通过在
中定义以下 Spring bean,让 Spring 识别上面的注册表grails-app/conf/spring/resources.grooovy
I found a solution I'm pretty happy with.
Step 1: Create an implementation of PropertyEditorSupport to convert text to/from the relevant Enum
Step 2: Define a class that registers EnumEditor as a converter for the various enum classes. To change the list of enum classes that are bindable by id, just modify
BINDABLE_ENUMS
Step 3: Make Spring aware of the registry above by defining the following Spring bean in
grails-app/conf/spring/resources.grooovy
因此,默认的数据绑定绑定在枚举名称上,而不是绑定在枚举的单独定义的属性上。您可以按照您提到的方式创建自己的PropertyEditor,或者执行类似于以下的解决方法这:
So the default Databinding binds on the Enum name and not a separately defined property of the Enum. You can either create your own PropertyEditor as you have mentioned or do a work-around similar to this: