Grails iterating in gsp vs. accessing Map elements

点点圈 提交于 2019-12-06 10:22:07

问题


Full context: I'm trying to process multiple files using a grails Application. The code I will display comes from the post-processing page where it gives information about the files processed.

My initial sense was to use code like this:

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

Now, what I don't understand is why the contents displayed in the <g:each loop always reports key=value such as ExcelRows=14 as I have received in one output case.

When I switch comments (note the <%-- tag being used) it works exactly as expected. From my "ExcelRows" column, I get just "14." What is wrong with my thinking that the <g:each loop should do the same thing? Intuitively it comes down to For each item in fileContents display item.

My controller code:

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

//...snipped

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

        [fileContents:fileContents]
    }

回答1:


When iterating over a map you'll be working with Entrys. Try using:

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

Or

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


来源:https://stackoverflow.com/questions/6996043/grails-iterating-in-gsp-vs-accessing-map-elements

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!