简体   繁体   中英

Grails, how can I pass list of object from gsp to controller

I have a list of csv files that will be imported in database. So, in the first step, I display the name of files in the jsp page and then I wait the choice of user what's the file need to import it or to ignore it.

when the user confirms his response, I need to pass the list of files that user has chosen to import it to controller.

I thought about that : I set the list that contains a list of file in hidden field and i will recuperate it into controller action from form submit. But in the controller, it is read like a string variable and I can not extract data from it.

<g:hiddenField id="list_file_notimported" name="list_file_notimported" value="${list_file_notimported}" />
<table>
      <g:findAll in="${list_file_notimported}" expr="1" >
                  <tr>
                  <td></td>
                  <td>${it.code}</td>
                  <td>${it.name}</td>
                  <td><g:radio id="group_${it.id}" name="group_${it.id}" value="import" checked="${false}"  /></td>
                  <td><g:radio id="group_${it.id}" name="group_${it.id}" value="ignore" checked="${false}"  /></td>
                   </tr>                    
                 </g:findAll></table>

Any idea please?

Thanks.

This example works in Grails 2.4.3:

In the controller, define index action to return a model and selection action to compute the list of files selected in the gsp:

class FileListController {

    def index() { 
        [ list_file_notimported : [ 'a.csv', 'b.csv', 'c.csv', 'd.csv'] ]
    }

    def selection() {
        def selectedfiles = []
        params.keySet().each { String key ->
            if (key.startsWith("group_") && key.endsWith(".csv") && params[key] == "import") {
                selectedfiles << key.substring(6)
            }
        }
        render(selectedfiles)
    }

}

And in the view, create a form for the selection:

 <g:form action="selection" method="get">
 <table>
    <tr><th>Name</th><th>Import</th><th>Ignore</th></tr>
    <g:each var="it" in="${list_file_notimported}" >
        <tr>
            <td>${it}</td>
            <td><g:radio name="group_${it}" value="import"/></td>
            <td><g:radio name="group_${it}" value="ignore" checked="true"/></td>
        </tr>
    </g:each>                    
 </table>
 <g:actionSubmit value="selection"/>
 </g:form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM