Grails在gsp中迭代与访问地图元素
完整上下文:我正在尝试使用grails应用程序处理多个文件。我将显示的代码来自post-processing页面,其中提供了有关已处理文件的信息 我最初的感觉是使用这样的代码:Grails在gsp中迭代与访问地图元素,grails,gsp,Grails,Gsp,完整上下文:我正在尝试使用grails应用程序处理多个文件。我将显示的代码来自post-processing页面,其中提供了有关已处理文件的信息 我最初的感觉是使用这样的代码: <table> <tr> <th>Parsed from Excel:</th> <th>Uploaded to DS:</th> <th>File Name:</
<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>
//…剪断
def fileContents = [
ExcelRows:"${ods.numberOfRows}",
policies:"${ods.numberOfPolicies}",
originalFileName: "${ods.originalFilename}",
Size:"${ods.size}"
]
[fileContents:fileContents]
}
当在地图上迭代时,您将使用s。尝试使用:
<g:each in="${fileContents}" var="item">
<td>${item.value?.encodeAsHTML()}</td>
</g:each>
${item.value?.encodeAsHTML()}
或
${item?.encodeAsHTML()}
您可以发布控制器代码吗?这就是将模型“fileContents”返回到您的视图@Arthur Neves添加了相关的控制器代码,因此,您的对象文件内容是一个映射而不是列表!这就是为什么要显示关键点=value@Arthur,我应该如何在gsp中迭代映射而不嵌入groovy/Java代码?Rob Hruska的第二个建议如何${item?.encodeAsHTML()}您的第一个示例没有打印任何内容,第二个示例在${fileContents.values()}
上失败。我缺少什么吗?您确定视图中的fileContents
确实设置为非空值吗?是的,我可以使用${fileContents.ExcelRows?.encodeAsHTML()直接访问元素
所以我知道它就在那里。这太奇怪了。在我的第一个示例中,如果将${fileContents}
替换为${fileContents.entrySet()}
,fileContents
必须为空,则会发生什么情况。
<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>