完整上下文:我正在尝试使用 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
.
我的 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/