Grails iterating in gsp vs. accessing Map elements

12,739

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>
Share:
12,739
avgvstvs
Author by

avgvstvs

I'm currently studying for a Master's in Information Assurance and plan to concentrate on software security. I'm here to try (and sometimes fail) answering questions; to deepen my knowledge and meet people to code projects with. I'm a contributor helping to resurrect the OWASP ESAPI java project, and have been answering esapi-related questions on SO for a couple years now. I enjoy reverse-engineering, in the rare chances that I can do it.

Updated on June 05, 2022

Comments

  • avgvstvs
    avgvstvs almost 2 years

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