Grails iterating in gsp vs. accessing Map elements
When iterating over a map you'll be working with Entry
s. 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>
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, 2022Comments
-
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 reportskey=value
such asExcelRows=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 toFor 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] }