Grails在gsp中迭代和访问Map元素 [英] Grails iterating in gsp vs. accessing Map elements

查看:128
本文介绍了Grails在gsp中迭代和访问Map元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

完整的上下文:我正在尝试使用grails应用程序来处理多个文件。我将显示的代码来自后处理页面,其中提供了有关所处理文件的信息。



我的初衷是使用如下代码:

 < ;表> 
< 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 = {
def uploadedFile = request.getFile('excelFile')
$ b

// ...剪切

  def fileContents = [
ExcelRows:$ {ods.numberOfRows},
policies:$ {ods.numberOfPolicies},
originalFileName:$ {ods.originalFilename},
大小:$ {ods.size}
]

[fileContents:fileContents]
}
pre>

解决方案

在迭代地图时,您将使用 条目 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 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]
    }

解决方案

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>

这篇关于Grails在gsp中迭代和访问Map元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆