我有一个用例,我需要首先在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
有任何想法吗 ????
解决方法
这就是我过去的做法.这样你就得到了i18n的支持.
普遍优惠制
<g:select name="duration" from="${MyEnum.values()}" valueMessagePrefix="ENUM.MyEnum" />
messages.properties
ENUM.MyEnum.MIN15=15 Minutes ENUM.MyEnum.MIN30=30 Minutes ENUM.MyEnum.HOUR1=1 Hour ENUM.MyEnum.HOUR2=2 Hours ENUM.MyEnum.HOUR5=5 Hours ENUM.MyEnum.HOUR8=8 Hours ENUM.MyEnum.HALFDAY=half day ENUM.MyEnum.FULLDAY=full day