grails - 有没有更好的方法可以在Grails中返回并显示多个列表到 View ?

标签 grails grails-2.0 gsp

我有兴趣将多个列表返回到一个 View ,以便最终可以在一页上显示完全不同的查询中的信息行。我已经弄清楚如何做到以下几点:

这是我在 Controller 中执行的操作:

 def processMultipleLists()
 {

    def stirList = []

    Person stirling = new Person('Stirling','Crow', 47)
    Person lady = new Person('Lady','McShavers', 4)

    stirList << stirling
    stirList << lady

    def kathieList = []

    Person kathie = new Person('Kathie','Esquibel', 47)
    Person milagro = new Person('Milagro','Muffin', 4)
    Person meeko = new Person('Meeko','Muffin', 4)

    kathieList << kathie
    kathieList << milagro
    kathieList << meeko

    def returnThisMap = [:]
    returnThisMap.put('One', kathieList)
    returnThisMap.put('Two', stirList)


    return [returnMap : returnThisMap]
}

然后Grails将“returnMap”(包含“returnThisMap”,以下称为“mapNum”)返回到我的 View ,该 View 具有以下内容:
<g:if test="${returnMap.size() > 0}">

    <table border="1">

        <tbody>
          <g:each in="${returnMap}" status="i" var="mapNum">
            <g:if test="${mapNum.getKey() == 'One'}">
                <tr>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Favorite Number</th>

                </tr>

                <g:each in="${mapNum.getValue()}" status="c" var="listVar">
                    <tr>
                        <td>${listVar.firstName}</td>
                        <td>${listVar.lastName}</td>
                        <td>${listVar.favNumber}</td>

                    </tr>
                </g:each>



            </g:if>
            <g:elseif test="${mapNum.getKey() == 'Two'}">
                <tr>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Favorite Number</th>

                </tr>

                <g:each in="${mapNum.getValue()}" status="c" var="listVar">
                    <tr>
                        <td>${listVar.firstName}</td>
                        <td>${listVar.lastName}</td>
                        <td>${listVar.favNumber}</td>

                    </tr>
                </g:each>
            </g:elseif>


          </g:each>
        </tbody>

    </table>
</g:if>
<g:else>
    No records were found to display.
</g:else>

这实际上有效。它从两个列表中发布信息。但是...感觉有点“hacky”,因为我必须使用groovy标记来迭代returnMap中的键/对值。有没有更好的方法在Grails中显示多个列表?

最佳答案

返回到 View 的对象已经是一个 map ,因此无需创建另一个 map 。你可以这样做:

return [stirList: stirList, kathieList: kathieList]

然后,在您看来,您可以分别遍历每个对象:
<g:each in="${stirList}" var="stir">
    ...
</g:each>
<g:each in="${kathieList}" var="kathie">
    ...
</g:each>

在您的示例中,两个列表看起来都包含相同的类型,并且以完全相同的方式显示,因此甚至没有必要进行区分。

关于grails - 有没有更好的方法可以在Grails中返回并显示多个列表到 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25752636/

相关文章:

grails - Groovy和GSP中的Markdown超链接

grails - 在grails gsp中调用标签不起作用

unit-testing - 是什么导致 Groovy/Grails 单元测试中出现 "exception in phase ' 指令选择“”?

grails - 使用 grails 获取连接池状态

java - java对象在groovy中编码时可以访问它们的实例方法吗?

grails - RestBuilder插件。如何在不创建文件的情况下上传文件?

grails - 一个GSP中有多个<g:select>, Controller 内部的HQL中有多个参数

grails - grails:相同的物理表名称引用了多个逻辑表名称

hibernate - 缺少数据源时,如何防止Bootstrap数据迁移运行

jquery - Grails 远程函数 onSuccess 与 onComplete