[英]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.