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!
1) To retrieve the selected checkboxes, use something like this (supposing you're using JSP):
<input type="checkbox" name="YourName" id="checkbox-1" value="1" />
<input type="checkbox" name="YourName" id="checkbox-2" value="2" />
<input type="checkbox" name="YourName" id="checkbox-3" value="3" />
etc...
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:
<s:if test="'checkboxId' in selectedList">checked="checked"</s:if>
精彩评论