访问控制器中的参数列表 [英] Accessing a list of params in controller
问题描述
我非常新的grails(1.3.7)所以请耐心等待: - )
Im very new to grails (1.3.7) so please be patient :-)
我有一个gsp我有各种复选框。用户可以点击它们,然后将他的答案发送给控制器。
I have a gsp where I have various checkboxes. A user can click on them and then send his answer to a controller. The controller is receiving this request correctly.
我的问题是,为了处理用户选择的内容,我必须检查每个参数 - 看看这个复选框是否真的检查。这真的很繁琐,并且不工作很好,因为显示复选框的页面是动态的 - 所以可以点击的复选框是动态的。在我的控制器中,我不知道我必须检查哪些参数。
My problem is, that, for working with what the user chose, I have to check every parameter - to see if this checkbox was really checked. Thats really cumbersome and doesnt work very well, because the page displaying the checkboxes is dynamic - so the checkboxes which can be clicked are dynamic too. In my controller I dont know for which params I have to check then.
有可能接收所有复选框的列表(或更好:所有复选框)我的控制器?我研究了,但没有找到答案!
Is there any possibility to receive a list of all checkboxes (or better: all checked checkboxes) in my controller? I researched but didnt find an answer!
感谢回答! :-)
谢谢你,
params.name.each{i->
System.out.println(i);
}
是非常简单和工作:-)它只是回馈检查的
is very simple and works :-) It just gives back the checked ones
推荐答案
它必须作为一个额外的请求参数传递(这是http的限制)。您可以在表单中添加以下字段,例如:
It must be passed as an extra request parameter (it's a limitation of http). You can add following field into your form, for example:
<input type="hidden" name="checkboxes" value="${myCheckboxesNames.join(',')}"/>
或使用JavaScript制作,因为它的名称在客户端是动态的。
or making same using JavaScript, as it names are dynamic on client side.
BTW,您还可以通过
BTW, you can also check all request parameters, by
params.each { name, value ->
// so something
}
所以如果你使用一些特殊的前缀/ suffix为这个复选框的名称,它将是:
so if you are using some special prefix/suffix for this checkbox names, it would be:
params.entrySet().findAll {
it.key.startsWith(prefix)
}.each {
println "Checkbox $it.key = $it.value"
}
这篇关于访问控制器中的参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!