2017-03-02 70 views
1

如何通过使用Grails从message.properties文件中读取数据来生成下拉列表?我已经创建了域文件:通过Grails中的属性文件读取数据创建下拉列表

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

这里存储在message.properties我的数据文件

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

如何显示使用标签库中GSP的信息?

回答

0

要获得基于给定参数的消息,你可以使用

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

从枚举列表生成一个下拉列表基本上如下进行:

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

但是你想拥有属性消息作为一个值。您还可以使用message从标签库作为

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

所以解决您的问题将是,例如

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

因为不知何故,我不能让它与<g:select>工作,但最终也生成纯粹的HTML。 只记得在属性中有所有相应的消息。

+0

感谢您的回答,我解决了它! –

+0

@KCKoay很高兴帮助!如果它解决了您的问题,您现在可以将此答案标记为已接受(并且如果您愿意,请将其提高)。 谢谢! –

+0

对不起,我不能赞成,因为我的声望小于1,但我将这个答案标记为接受。再次感谢您的帮助! –