0
votes

I have a typical java string array String[] token = new String[20] in a controller. I want to pass this array to .gsp file, where all the elements in token get printed out with a line break. Wasn't too long before I figured that <g:each> should do exactly what needed. However, after attempting multiple options, nothing seems to have worked so far.

http://grails.org/doc/latest/ref/Tags/each.html This guide shows some examples, however, none of them seem to suit my need, because I need to read an array, not display different domain variables (and I actually tried them without luck).

Grails iterating in gsp vs. accessing Map elements This answer did not work

http://grails.1312388.n4.nabble.com/How-to-display-the-contents-of-a-string-Array-in-GSP-td1355405.html This at first seemed quite close to what I needed, however that also didn't work.

I've spent hours already on trying different combinations and variations of all those things. Every time it either results in an error or no elements get printed out. I know they are there, because if I ask for them specifically (just for one element), I can see results.

In my controller, I've tried passing the token with render view:'myFancy.gsp', model:['lsOut':token] and return [lsOut:token]. (Even return ['lsOut':token] just to be sure)

I'm out of ideas. Any suggestions on what to try next or what I could try again (because maybe I did some mistake)?

Controller:

def readContents() 
{
    String s = new RunSshCommand().execute() {
        host = params.ServerAddress
        username = params.Username
        password = params.Password
        command = "ls -m Funk"
        }

    String[] token = new String[20]
    token = s.split(",")
    //render view:'readContents.gsp', model:['i':token]
    return [lsOut:token]
}

GSP file

    <ul>
        <g:each in="${token}" var="lsOut">
            <td>${lsOut.value?.encodeAsHTML()}</td>
        </g:each>
    </ul>
1
Can you also add what you have tried latest in the controller and gsp exactly? - dmahapatro
Sure, added the latest code. As you can see I was pretty desperate already and was trying basically anything. I'm not sure how much that helps though, because ideally I should show everything that I've tried, but I can't simply remember all my attempts anymore. And before anyone says that I shouldn't parse ls output - yes, I know, but server doesn't support SSHFS (at least can't count on it), so I'm open to suggestions on that as well. - Birman

1 Answers

1
votes

Your GSP file needs to get the list named lsOut in the model and iterate on all the elements. Note that with <td> all elements will appear in a single table row. You must use HTML separator <li> in order to display the elements on multiple lines in the <ul> list:

<ul>
    <g:each in="${lsOut}" var="it">
        <li>${it.encodeAsHTML()}</li>
    </g:each>
</ul>