struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Radu Solomon <>
Subject RE: Radio button grouping in Struts 2
Date Mon, 04 May 2009 12:25:40 GMT
You need to use Map backed properties if you want to get this to work.

Take a look at this example and you should be able to adapt it.

Basically your properties are a map with id and Question objects and then struts can repopulate
your answers in the correct question object in the map. It works very nice. I used it for
a page that had a random number of file uploads.


-----Original Message-----
From: Jon Pearson []
Sent: May 4, 2009 8:18 AM
To: Struts Users Mailing List
Subject: RE: Radio button grouping in Struts 2

> Hi all,
> I have a page which iterates over a collection of questions,
> with each question having a number of radio buttons which can
> be used to select the answer to that question. For example:
> Question 1:   Unanswered O  Yes O  No O
> Question 2:   Unanswered O  Yes O  No O
> etc..
> If the question has already been answered previously when the
> pages loads then the appropriate Yes/No is selected;
> otherwise Unanswered is selected.
> I've got this working fine using the following:
> class Question {
>   long id;
>   String name;
>   String answer;
> }
> class ViewQuestionsAction {
>   List<Question> questions;
>   Map<String,String> answersMap; // key == value at the
> moment because didn't seem to work with int keys
> <s:set name="answersMap" value="%{answersMap}"/> <s:iterator
> value="questions">
>   <s:property value="name"/>
>   <s:radio name="%{id}" value="%{answer}"
> list="#answersMap"/> </s:iterator>
> I need the name=%{id} bit so that I can group the radio
> buttons per question. And this is the problem, because then I
> can't find a way for when you submit the form to receive an
> array of the selected buttons into the target action. The
> above works on page load (grouped nicely), but on submit I
> get all kinds of OGNL errors complaining about long id
> invalid expressions (understandable I guess). But when I
> choose a sensible name, for example:
>   <s:radio name="responses" value="%{answer}" list="#answersMap"/>
> I get an array of selected buttons in the target action's
> responses property on submit but the radio grouping isn't
> working - you can only select on radio button from the whole
> page because they all share the same name element in the
> HTML. I then tried:
>   <s:radio id="ViewQuestions_responses" name="%{id}" value="%{answer}"
> list="#answersMap"/>
> To try and force struts to set the responses property, but it
> still tries to set the property from the name. Which makes me
> think: if struts always uses the name to set the property,
> and the browser always groups the radio buttons based on the
> same name, is what I'm trying to do even possible?!?!
> Any help gratefully received :)
> Many thanks
> James
> --
> View this message in context:
> 5502p23365502.html
> Sent from the Struts - User mailing list archive at
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

You could, instead of relying on Struts2 to automatically call the
proper set* routines on submit, implement Preparable and manually parse
the results in the prepare() function, which is called BEFORE any set*


To unsubscribe, e-mail:
For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message