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!

Daniel
  • 2,050
  • 7
  • 31
  • 55

1 Answers1

2

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> 
TBW
  • 128
  • 1
  • 9
  • Thanks for your answer! I´ll be using JSPs and the struts2 taglibs, but I guess your " tag. I´ll test the ArrayList solution for sure. Regarding my architecture, I´ll have one object holding the user information. That object will have a @OneToMany annotation for a Set, which will hold objects representing the answers selected by each user. So it would be very possible to fetch a list with the answers and use as a basis for the edit page. Regarding the , would I put that inside the checkbox tag? – Daniel Jan 26 '11 at 06:14
  • The thing is you cannot put a Struts2 tag inside another Struts2 tag (hence /> does not work). From experience, it is sometimes more convenient to use regular XHTML tags. If you want to use S2 tags, you can do something like: . To check the Struts2 checkbox you have to specify "true" for the value, which can be a pain (if you want to get another value in your action class for instance). This is because otherwise you should be using checkboxlists, but they are not always convenient either. – TBW Jan 26 '11 at 10:21
  • Ah then I get your point. Seems like this would do exactly what I want, and your approach with using XHTML tags would be much cleaner than having if/else tag with struts tags. I´ll be able to test this approach tomorrow, then I´ll get back with results! – Daniel Jan 26 '11 at 15:48