【问题标题】:Create Drop-down List By Reading Data From Properties File in Grails通过从 Grails 中的属性文件中读取数据来创建下拉列表
【发布时间】:2017-07-21 22:16:05
【问题描述】:

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

  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

如何使用taglib在GSP中显示信息?

【问题讨论】:

    标签: java grails groovy properties gsp


    【解决方案1】:

    要根据给定的参数获取消息,您可以使用

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

    ${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>
    

    因为不知何故我无法让它与&lt;g:select&gt; 一起工作,但最终它也会生成纯 HTML。 请记住在属性中包含所有相应的消息。

    【讨论】:

    • @KCKoay 很高兴有帮助!如果它解决了您的问题,您现在可以将此答案标记为已接受(如果您愿意,可以投票)。谢谢!
    • 抱歉,由于我的声誉小于 1,我无法投票,但我将此答案标记为已接受。再次感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多