Grails在gsp中迭代和访问Map元素 [英] Grails iterating in gsp vs. accessing Map elements
问题描述
我的初衷是使用如下代码:
< ;表>
< tr>
< th>从Excel解析:< / th>
th上传到DS:< / th>
< th>文件名:< / th>
< th>大小:< / th>
< / tr>
< tr>
< td> $ {item}< / td>
< / g:每个>
<% -
< td> $ {fileContents.ExcelRows?.encodeAsHTML()}< / td>
< td> $ {fileContents.policies?.encodeAsHTML()}< / td>
< td> $ {fileContents.originalFileName?.encodeAsHTML()}< / td>
< td> $ {fileContents.Size?.encodeAsHTML()}< / td>
- %>
< / tr>
< / table>
现在,我不明白为什么显示在 < g:每个
循环总是会报告键=值
,例如 ExcelRows = 14
as我收到了一个输出案例。
当我切换评论时(注意<% -
>标记为使用)它完全按预期工作。从我的 ExcelRows
列中,我只得到了14。我的想法出了什么问题:< g:each
循环应该做同样的事情?直观地,它归结为对于fileContents中的每个项目
。
显示项目
我的控制器代码:
def processFile = {
$ b
def uploadedFile = request.getFile('excelFile')
// ...剪切
def fileContents = [
pre>
ExcelRows:$ {ods.numberOfRows},
policies:$ {ods.numberOfPolicies},
originalFileName:$ {ods.originalFilename},
大小:$ {ods.size}
]
[fileContents:fileContents]
}
解决方案在迭代地图时,您将使用
条目
s。尝试使用:
< g:each in =$ {fileContents}var =item>
< td> $ {item.value?.encodeAsHTML()}< / td>
< / g:每个>
或
< g:each in =$ {fileContents.values()}var =item>
< td> $ {item?.encodeAsHTML()}< / td>
< / g:每个>
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] }
解决方案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>
这篇关于Grails在gsp中迭代和访问Map元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!