从列表中获取/设置多个 struts2 复选框
我目前正在开发一个 Struts2 应用程序,用户将在其中做出三个选择之一,并根据该选择显示许多不同的预定义复选框。无论做出什么选择,某些复选框都会出现,而其他复选框对于每个可以做出的选择都是唯一的。目前总共有大约 50 个不同的复选框,每个复选框都有一个 ID,从 1 到 50。
用户所做的选择将作为行保存在数据库中,我需要能够重新填充选定的复选框如果用户想要更改所做的选择。
使用复选框列表是不合适的,因为我需要将复选框放入页面上的不同字段集中。所以我想我只能坚持常规的了。
现在,有人知道我该如何: 1)检索选定的复选框,最好将它们放入我的操作中的列表/集中 2) 从存储选择的列表中重新填充选定的复选框?
任何想法都非常感激!
I´m currently developing a Struts2 app where a user will make one of three choices, and based on the choice a number of different predefined checkboxes will be presented. Some of the checkboxes will appear regardless of the choice made, while others will be unique for each choice that can be made. In total, there are currently around 50 different checkboxes, and each checkbox will have an ID, from 1 to 50.
The selections made by the user will be persisted as rows in a database, and I need to be able to repopulate the selected checkboxes should the user want to change the selections made.
Using a checkboxlist is not suitable, as I need to put the checkboxes into different fieldsets on the page. So I guess I´m stuck with the regular .
Now, has anybody got a good idea of how I could:
1) Retrieve the selected checkboxes, preferebly by putting them into a list/set in my action
2) Repopulate the selected checkboxes from a list where the selections has been stored?
Any ideas are much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1) 要检索选定的复选框,请使用类似以下内容(假设您使用的是 JSP):
在 Action 类中,您可以检索变量“YourName”作为整数的 ArrayList,例如,它将自动填充选中的复选框Struts2 的复选框 ID。
2) 例如,要重新填充编辑页面上的复选框,有许多可能的解决方案(您能给我们一些有关您的架构、对象等的更多详细信息吗?)
在您的 Action 类中,您可以在列表中检索 id以前选中的复选框(我们称之为 selectedList)。一旦进入 JSP 视图,您就可以将“checked="checked"”参数添加到复选框字段,条件是该框的 ID 包含在 selectedList 中。类似的东西应该有效:
1) To retrieve the selected checkboxes, use something like this (supposing you're using JSP):
In your Action class, you can retrieve the variable "YourName" as an ArrayList of integers for instance, it will be automatically populated with the checked checkbox IDs by Struts2.
2) To repopulate the checkboxes on an Edit page for instance, there are many possible solutions (could you give us some more details about your architecture, objects, and so on?)
In your Action class, you could retrive in a list the ids of formerly-checked checkboxes (let's call it selectedList).Once on your JSP view, you could add the "checked="checked"" parameter to your checkbox field on the condition that the ID of the box is contained in selectedList. Something like that should work: