Grails迭代gsp与访问Map元素

时间:2011-08-09 12:15:29

标签: 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=value,例如我在一个输出案例中收到的ExcelRows=14

当我切换评论时(请注意正在使用的<%--标记),它的工作方式完全符合预期。从我的“ExcelRows”列开始,我只得到“14”。我认为<g:each循环应该做同样的事情有什么问题?直观地,它归结为For each item in fileContents display item

我的控制器代码:

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

// ...剪断

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

        [fileContents:fileContents]
    }

1 个答案:

答案 0 :(得分:9)

在迭代地图时,您将使用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>