简体   繁体   English

Grails,如何将对象列表从gsp传递到控制器

[英]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. 我有一个将导入数据库的csv文件列表。 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. 因此,在第一步中,我将在jsp页面中显示文件名,然后等待用户选择,导入或忽略该文件需要什么文件。

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: 此示例在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: 在控制器中,定义index操作以返回模型,并selection操作以计算在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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Grails:通过远程链接将列表从GSP传递到控制器 - Grails: pass a List from GSP to controller with remote link 如何在Grails remoteFunction中将参数从gsp传递到控制器作为参数的一部分 - How to pass a map from gsp to controller as part of params in Grails remoteFunction 如何将参数传递给我从gsp获得的服务以及Grails中的Controller中拥有的参数 - How to pass params into Services that I get from the gsp and I have in my Controller in Grails 如何在Grails中的GSP和控制器之间传递数据而不将其存储在数据库中? - How can I pass data between GSP and controller in Grails without storing in the database? 将复选框值从Grails中的GSP传递给控制器 - Pass checkbox value to controller from GSP in Grails grails gsp变量:如何在gsp中作为控制器方法的参数传递 - grails gsp variables: how to pass as parameter of controller method in gsp 如何将HashMap传递给gsp页面然后再传回控制器? - How can I pass a HashMap to gsp page and then back to the controller? 如何在g:form中将一些值从gsp传递到控制器? - How can I pass some values from gsp to controller in a g:form? grails,将数据从控制器传递到生成的_form.gsp视图 - grails, pass data from controller to generated _form.gsp view 如何将g:textfiled值从GSP传递到grails中作为参数的一部分 - How to pass g:textfiled value from GSP to controller in grails as part of params
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM