如何将地图列表传递给GSP页面视图并对其进行迭代 [英] How to pass a List of Maps to a GSP page view and iterate over it

查看:190
本文介绍了如何将地图列表传递给GSP页面视图并对其进行迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们想要显示关于文件夹文件的信息。我们必须将每个文件的信息保存在一个Map中。然后,将这些地图添加到列表中。

控制器操作:

  def show(){

列出结果= new ArrayList();

文件dir = getDir(params.id);

if(dir.exists()){
dir.eachFile {

Map fileInformation = new java.util.LinkedHashMap()

fileInformation.put(name,it.getName());
fileInformation.put(size,it.length());
fileInformation.put(path,it.getAbsolutePath());

results.add(fileInformation);



$ files_folderData $ result
$ b $ / code>

也许,这是我在视图中获取数据的最佳尝试(我遵循这里没有运气):

 < g:每个in =$ {filesOfFolderData}> 

< p> it:$ {it}< / p>
< p> it.properties:$ {it.properties}< / p>


< p> propertyEntry.key:$ {propertyEntry.key}< / p>
< p> propertyEntry.value:$ {propertyEntry.value}< / p>
< p> propertyEntry.value.name:$ {propertyEntry.value}< / p>

< / g:每个>

< / g:每个>

这是互联网浏览器显示的内容(注意:结果的第一行可能有点儿不同的,因为我简化了代码,所以我的客人,结果在我的案件的真实结果的基础):

$ $ p $ it:[{ name = wololo1,size = 35,path = c:\},{name = wololo2,size = 35,path = c:\}]

it.properties:{class = class java .util.ArrayList,empty = false}

propertyEntry.key:class
$ b propertyEntry.value:类java.util.ArrayList
$ b $ propertyEntry。 value.name:类java.util.ArrayList

propertyEntry.key:空

propertyEntry.value:false

propertyEntry.value.name: false

我们如何迭代列表?

解决方案

通过地图中的每个,您可以访问 value ,因此只需迭代该值即可。

 < g:每个in =$ {filesOfFolderData} VAR = 文件 > 
...
< / g:每个>
< / g:每个>


Let's say we want to show information about the files of a folder. We have to save the information of each file in a Map. Then, add these Maps to a List.

Controller action:

def show() {

    List results = new ArrayList();

    File dir = getDir(params.id);

    if (dir.exists()) {
        dir.eachFile { 

        Map fileInformation= new java.util.LinkedHashMap()

        fileInformation.put("name", it.getName());
        fileInformation.put("size", it.length());
        fileInformation.put("path", it.getAbsolutePath() );

        results.add(fileInformation);

        }
    }

    [filesOfFolderData: result]
}

Maybe, this is my best attempt to get the data in the view (I followed the approach of here with no luck):

<g:each in="${filesOfFolderData}">

    <p> it: ${it}</p>
    <p> it.properties: ${it.properties} </p>

    <g:each var="propertyEntry" in="${it.properties}">

        <p> propertyEntry.key: ${propertyEntry.key} </p>
        <p> propertyEntry.value: ${propertyEntry.value} </p>
        <p> propertyEntry.value.name: ${propertyEntry.value} </p>           

    </g:each>

</g:each>

This is what the Internet Browser shows (note: the first line of the result could be a little bit different as I simplify the code so I guest that result in base of the real result of my case):

it: [{name=wololo1, size=35, path=c:\}, {name=wololo2, size=35, path=c:\}]

it.properties: {class=class java.util.ArrayList, empty=false}

propertyEntry.key: class

propertyEntry.value: class java.util.ArrayList

propertyEntry.value.name: class java.util.ArrayList

propertyEntry.key: empty

propertyEntry.value: false

propertyEntry.value.name: false 

How could we iterate over the List?

解决方案

With each in maps you have access to the key and value, so just iterate over the value.

<g:each in="${filesOfFolderData}" var="files">
  <g:each in="${files.value}" var="file">
    ...
  </g:each>
</g:each>

这篇关于如何将地图列表传递给GSP页面视图并对其进行迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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