在 GSP 页面上显示枚举值,然后将它们绑定到数据库中
我有一个用例,其中我需要首先在 GSP 页面上将 enum
的值显示为下拉列表,让用户选择其中一个值,然后最终将数据绑定到领域。
所以我在 GSP 上的代码看起来像我的枚举是 MyEnum
<g:select from="${MyEnum.getAllEnumList()}" optionValue="name" name="duration"/>
我的枚举是
public enum MyEnum {
MIN15('15 Minutes'),
MIN30('30 Minutes'),
HOUR1('1 Hour'),
HOUR2('2 Hours'),
HOUR5('5 Hours'),
HOUR8('8 Hours'),
HALFDAY('half day'),
FULLDAY('full day')
private final String name
private final String displayName
public static final List<MyEnum> getAllEnumList() {
[MIN15,MIN30,HOUR1,HOUR2,HOUR5,HOUR8,HALFDAY,FULLDAY]
}
public String toString() {
return displayName
}
MyEnum(String name,String displayName) {
this.name = name
this.displayName = displayName;
}
}
当我点击页面时它显示一个错误,例如:
Error processing GroovyPageView: Error executing tag <g:form>: Error evaluating expression [MyEnum.getAllEnumList()] on line [37]: java.lang.NoClassDefFoundError: Could not initialize class ENUM.MyEnum at D:/myspace/projects/IcepushpluginSampleApp/grails-app/views/util/test.gsp:46
任何想法????
I have a use case in which I need to first show the value of enum
on the GSP page first as a drop down list, have the user select one of those values and then finally bind the data to the domain.
So my code on GSP looks like my enum is MyEnum
<g:select from="${MyEnum.getAllEnumList()}" optionValue="name" name="duration"/>
my enum is
public enum MyEnum {
MIN15('15 Minutes'),
MIN30('30 Minutes'),
HOUR1('1 Hour'),
HOUR2('2 Hours'),
HOUR5('5 Hours'),
HOUR8('8 Hours'),
HALFDAY('half day'),
FULLDAY('full day')
private final String name
private final String displayName
public static final List<MyEnum> getAllEnumList() {
[MIN15,MIN30,HOUR1,HOUR2,HOUR5,HOUR8,HALFDAY,FULLDAY]
}
public String toString() {
return displayName
}
MyEnum(String name,String displayName) {
this.name = name
this.displayName = displayName;
}
}
when I hit the page its showing an error like:
Error processing GroovyPageView: Error executing tag <g:form>: Error evaluating expression [MyEnum.getAllEnumList()] on line [37]: java.lang.NoClassDefFoundError: Could not initialize class ENUM.MyEnum at D:/myspace/projects/IcepushpluginSampleApp/grails-app/views/util/test.gsp:46
Any ideas ????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我过去就是这样做的。这样你就有了 i18n 支持。
gsp
消息.properties
This is how I have done it in the past. This way you have i18n support.
gsp
messages.properties
如果您使用 自定义标签库。如果您希望选项键与其值不同,您还需要向
enum
添加另一个方法 (getKey()
)。MyEnum.groovy
MyEnumTagLib.groovy
gsp
You can avoid importing in your GSP (which is kind of ugly) if you use a custom tag library. You'll also need to add another method (
getKey()
) to yourenum
if you want to have the option key be different from its value.MyEnum.groovy
MyEnumTagLib.groovy
gsp
在 GSP 顶部尝试此操作(当然,根据您的包调整完全限定路径)。
编辑(这应该有效(你的枚举语法也是错误的)):
然后修改后的类:
Edit2:
避免整个事情的最简单方法(这里的第二个答案和我的解决方案)是将值列表简单地传递给来自控制器的 gsp。只需添加
或类似于您的控制器返回的内容即可。
Try this at the top of your GSP (with the fully qualified path adjusted to your packages, of course).
Edit (this should work (your enum syntax is wrong too)):
And then the revised class:
Edit2:
The easiest way to avoid the whole thing (both the second answer here and my solution) is to simply pass off the value list to the gsp from the controller. Simply add
or something similar to your controller return.