struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "jdt.me.uk" <jdt.me...@gmail.com>
Subject Radio button grouping in Struts 2
Date Mon, 04 May 2009 09:25:11 GMT


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: http://www.nabble.com/Radio-button-grouping-in-Struts-2-tp23365502p23365502.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message