How to pass a List of Maps to a GSP page view and iterate over it

北城以北 提交于 2019-12-11 19:26:41

问题


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?


回答1:


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>



回答2:


Well, you can use the code given below: Example:

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

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

Output:

name: wololo1
size: 35
path: c:\
name: wololo2
size:35
path: c:\

Hope that helps!!!

Thanks




回答3:


Instead of a List of Maps, we have to create a Map of Maps:

def show() {

    def results = []
    File dir = getDir(params.id);

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

            results <<  [
                            name: it.getName(),
                            size: it.length(),
                            it.getAbsolutePath()
                        ]                   
        }
    }

    [filesOfFolderData: result]
}

Then, the solution of @SergioMichels will work perfectly (I copy&paste his solution):

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


来源:https://stackoverflow.com/questions/18598486/how-to-pass-a-list-of-maps-to-a-gsp-page-view-and-iterate-over-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!