Grails 在 gsp 中迭代与访问 Map 元素

标签 grails gsp

完整上下文:我正在尝试使用 grails 应用程序处理多个文件。我将显示的代码来自后处理页面,其中提供了有关已处理文件的信息。

我最初的感觉是使用这样的代码:

    <table>
      <tr>
        <th>Parsed from Excel:</th>
        <th>Uploaded to DS:</th>
        <th>File Name:</th>
        <th>Size:</th>
      </tr>
      <tr>
      <g:each in="${fileContents}" var="item">
            <td>${item}</td>
      </g:each>
        <%-- 
        <td>${fileContents.ExcelRows?.encodeAsHTML()}</td>
        <td>${fileContents.policies?.encodeAsHTML()}</td>
        <td>${fileContents.originalFileName?.encodeAsHTML()}</td>
        <td>${fileContents.Size?.encodeAsHTML()}</td>
        --%>
      </tr>
    </table>

现在我不明白的是为什么<g:each中显示的内容循环始终报告 key=valueExcelRows=14正如我在一个输出案例中收到的那样。

当我切换评论时(请注意正在使用的 <%-- 标签),它完全按照预期工作。从我的“ExcelRows”列中,我只得到“14”。我的想法有什么问题 <g:each循环应该做同样的事情吗?直观上它可以归结为 For each item in fileContents display item .

我的 Controller 代码:

def processFile = {
        def uploadedFile = request.getFile('excelFile')

//...被剪掉

        def fileContents = [
            ExcelRows:"${ods.numberOfRows}",
            policies:"${ods.numberOfPolicies}",
            originalFileName: "${ods.originalFilename}", 
            Size:"${ods.size}"
            ]

        [fileContents:fileContents]
    }

最佳答案

迭代 map 时,您将使用 Entry s。尝试使用:

<g:each in="${fileContents}" var="item">
   <td>${item.value?.encodeAsHTML()}</td>
</g:each>

或者

<g:each in="${fileContents.values()}" var="item">
   <td>${item?.encodeAsHTML()}</td>
</g:each>

关于Grails 在 gsp 中迭代与访问 Map 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6996043/

相关文章:

grails - 在 DataSources.groovy 中扩展多个 grails 数据源?

grails - Grails UI性能插件页面压缩问题

html - 从 JSON 数组读取到 Grails HTML Select 标签

css - 如何在 gsp grails 中动态更改左/下人字形

grails - 如何传递和显示JSONArray到GSP页面以显示?

css - 使用 twitter bootstrap 在一行中挤压按钮

grails - 将i18n消息作为参数传递给Grails模板

grails - 在GSP grails View 中乘以参数

Grails 3.2.4, IntelliJ 2016.3.4 有一个为脚本代码生成的合成类

grails - 学习 Grails 的动手学习计划