Show enumeration values ​​on the GSP page and then link them to the database

I have a usage example in which I need to first show the enum value on the GSP page first as a drop-down list, select a user from one of these values, and then bind the data to the domain.

So my code in GSP is similar to my enumeration MyEnum

 <g:select from="${MyEnum.getAllEnumList()}" optionValue="name" name="duration"/> 

my listing

 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 click on a page with its error, for example:

 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 ????

+7
source share
3 answers

Here's how I did it in the past. Thus, you support i18n.

Gsp

 <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 
+9
source
 <%@ page import="fully.qualified.path.MyEnum" %> 

Try this at the top of your GSP (of course, with the full path adjusted for your packages).

Edit (this should work (your enum syntax is also incorrect)):

 <%@ page import="ENUM.MyEnum" %> <html> <head> </head> <body> <g:select from="${MyEnum.getAllEnumList()}" optionValue="displayName" name="duration"/> </body> </html> 

And then the revised class:

 package ENUM 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 displayName public static final List<MyEnum> getAllEnumList() { [MIN15,MIN30,HOUR1,HOUR2,HOUR5,HOUR8,HALFDAY,FULLDAY] } public String toString() { return displayName } MyEnum(String displayName) { this.displayName = displayName; } } 

Edit2:

The easiest way to avoid all this (both the second answer here, and my solution) is to simply transfer the list of values ​​to gsp from the controller. Just add

 [duration:MyEnum.values()] 

or something similar to returning your controller.

+2
source

You can avoid importing into your GSP (which is pretty ugly) if you use a custom tag library . You also need to add another method ( getKey() ) to your enum if you want the option key to be different from its value.

MyEnum.groovy

 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') final String displayName private MyEnum(String displayName) { this.displayName = displayName } String getKey() { name() } String toString() { displayName } } 

MyEnumTagLib.groovy

 // add import if MyEnum is in a different package class MyEnumTagLib { static namespace = 'my' def enumSelect = { attrs -> attrs.from = MyEnum.values() attrs.optionKey = 'key' out << g.select(attrs) } } 

Gsp

 <my:enumSelect name="duration"/> 
+2
source

All Articles