Search code examples
javagrailsgroovypropertiesgsp

Create Drop-down List By Reading Data From Properties File in Grails


How to generate drop-down list by reading data from message.properties file using Grails? I already created the domain file:

  class Feedback {

    enum Type {
    COMPT("compt") ,
    COMPL("compl") ,
    ENQ("enq")

    final String typeID 
    Type (String typeID){
        this.typeID = typeID
    }
    String toString(){
        typeID
    } 
}



    static constraints = {
        typeID inList: Type.values()*.typeID
       }
}

Here is my data stored in message.properties file

  type.compt=Complaint
  type.compl=Compliment
  type.enq=Enquiry

How to display the information in GSP using taglib?


Solution

  • To get the message based on the given parameter, you could use

    <g:message code="type.${passedFeedbackType}" />
    

    Generating a dropdown list from a enum list is basically done as following:

    <g:select name="yourList" value="${value_you_want_to_have_first}" 
              from="${Feedback.Type.values()}" optionValue="${it}" 
    optionKey="typeId"/>
    

    But you want to have the property message as a value. You can also use message from the taglib as

    ${g.message(code:'your.message.code')}
    

    So the solution to your problem would be, for example

    <select>
        <g:each in="${Feedback.Type.values()}" var="feedbackType">
            <option value="${type}">${g.message(code:"type.${type}")}</option>
        </g:each>
    </select>
    

    Because somehow I can't get it working with <g:select>, but eventually it also generates pure HTML. Just remember to have all the corresponding messages in the properties.