using value of enum in g:select when enum is attribute of selection object

不问归期 提交于 2019-12-05 01:48:12
Gregg
enum BatchRange {
    JAN1 "January Biweekly 1",
    JAN2 "January Biweekly 2",

    final String value

    BatchRange(String value) { this.value = value }

    String toString() { value } 
    String getKey() { name() }
}

Note the getKey() method. And then your g:select

<g:select name="batch" from="${BatchRange.values()}" optionKey="key" />

or

<g:select name="batch" from="${BatchRange.values()}" keys="${BatchRange.values()*.name()}" />

A better approach would be to use i18n messages in this case. There are two options:

  1. Add valueMessagePrefix to the select.
  2. Make the enum implement org.springframework.context.MessageSourceResolvable as described in this blog post.

See this question for further information.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!