You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "jdt.me.uk" <jd...@gmail.com> on 2009/05/04 11:25:11 UTC

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: 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


RE: Radio button grouping in Struts 2

Posted by Martin Gainty <mg...@hotmail.com>.
the only suggestion i can think of is rework existing List to a Map e.g. possibleAnswersMap so 
value is the value part of entry from possibleAnswersMap
key is key part of entry from possibleAnswersMap

otherwise looks good
Martin Gainty 
______________________________________________ 
Jogi és Bizalmassági kinyilatkoztatás/Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
 Ez az
üzenet bizalmas.  Ha nem ön az akinek szánva volt, akkor kérjük, hogy
jelentse azt nekünk vissza. Semmiféle továbbítása vagy másolatának
készítése nem megengedett.  Ez az üzenet csak ismeret cserét szolgál és
semmiféle jogi alkalmazhatósága sincs.  Mivel az electronikus üzenetek
könnyen megváltoztathatóak, ezért minket semmi felelöség nem terhelhet
ezen üzenet tartalma miatt.

Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.




> Date: Mon, 18 May 2009 02:22:06 -0700
> From: nataraja.cp@cognizant.com
> To: user@struts.apache.org
> Subject: RE: Radio button grouping in Struts 2
> 
> 
> Hi,
> 
>     Can you please attach the sample source code for this? even I am facing
> the same problem. I am new to Struts, so need some detailed example.
> 
> 
> Thanks in advance,
> 
> 
> ===============================================================================
> 
> jdt.me.uk wrote:
> > 
> > 
> > Hi Radu. Many thanks for your reply, and sorry for not responding sooner
> > as I've only just got around to looking at your link.
> > 
> > It definitely helped and I've got it working now. It's slightly different
> > and doesn't rely on Preparable:
> > 
> > class Question
> > {
> >     long id;
> >     String name;
> > }
> > 
> > public class ViewQuestionsAction
> > {
> >     // NOTE - getters required for all these properties
> >     List<Question> questions;
> >     List<String> answersList;  
> >     Map<Long, String> answersMap;
> > }
> > 
> > jsp:
> > <s:set name="answersList" value="%{answersList}"/>
> > <s:set name="answersMap" value="%{answersMap}"/>
> > <s:iterator value="questions" status="question_stat">  
> >     <s:property value="name"/><br/>
> >     <s:radio name="answers[%{id}]" value="%{#answersMap[id]}"
> > list="#answersList" />
> > </s:iterator>
> > 
> > public class SaveAnswersAction
> > {
> >     // NOTE - getter AND setter required for this map
> >     private Map<Long, String> answersMap;
> > }
> > 
> > In the ViewQuestionsAction (which sources the JSP):
> > 1) loads the questions from the DB;
> > 2) populates all the possible answers into the answersList
> > 3) populates the map with the current answers, keyed by question id
> > 
> > When the page loads, the current answer is selected in the radio (due to
> > value selecting the answer from the answersMap), and the radios are unique
> > by question because the name is unique by question (due to the map keyed
> > by question). 
> > 
> > When you click save, the SaveAnswersAction.answersMap map is populated
> > with the selected answers, keyed by the question's id (no prepareable
> > method needed). You can then do all the validation you need and update the
> > DB as appropriate.
> > 
> > I believe this is a really nice solution as loading entities from the DB
> > in a prepare() method seems wrong to me. I'd be interested to hear what
> > other people think. 
> > 
> > Many thanks
> > James
> > 
> > 
> > 
> > Rubbinio wrote:
> >> 
> >> 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.
> >> 
> >> http://www.vitarara.org/cms/struts_2_cookbook/updating_a_list_of_domain_entities
> >> 
> >> 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.
> >> 
> >> HTH
> >> Radu
> >> 
> >> -----Original Message-----
> >> From: Jon Pearson [mailto:Jon.Pearson@sixnet.com]
> >> 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:
> >>> http://www.nabble.com/Radio-button-grouping-in-Struts-2-tp2336
> >>> 5502p23365502.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
> >>>
> >> 
> >> 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*
> >> functions.
> >> 
> >> ~Jonathan
> >> 
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> For additional commands, e-mail: user-help@struts.apache.org
> >> 
> >> 
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> For additional commands, e-mail: user-help@struts.apache.org
> >> 
> >> 
> >> 
> > 
> > 
> 
> -- 
> View this message in context: http://www.nabble.com/Radio-button-grouping-in-Struts-2-tp23365502p23593931.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
> 

_________________________________________________________________
Hotmail® has ever-growing storage! Don’t worry about storage limits.
http://windowslive.com/Tutorial/Hotmail/Storage?ocid=TXT_TAGLM_WL_HM_Tutorial_Storage1_052009

RE: Radio button grouping in Struts 2

Posted by NR031 <na...@cognizant.com>.
Hi,

    Can you please attach the sample source code for this? even I am facing
the same problem. I am new to Struts, so need some detailed example.


Thanks in advance,


===============================================================================

jdt.me.uk wrote:
> 
> 
> Hi Radu. Many thanks for your reply, and sorry for not responding sooner
> as I've only just got around to looking at your link.
> 
> It definitely helped and I've got it working now. It's slightly different
> and doesn't rely on Preparable:
> 
> class Question
> {
>     long id;
>     String name;
> }
> 
> public class ViewQuestionsAction
> {
>     // NOTE - getters required for all these properties
>     List<Question> questions;
>     List<String> answersList;  
>     Map<Long, String> answersMap;
> }
> 
> jsp:
> <s:set name="answersList" value="%{answersList}"/>
> <s:set name="answersMap" value="%{answersMap}"/>
> <s:iterator value="questions" status="question_stat">  
>     <s:property value="name"/><br/>
>     <s:radio name="answers[%{id}]" value="%{#answersMap[id]}"
> list="#answersList" />
> </s:iterator>
> 
> public class SaveAnswersAction
> {
>     // NOTE - getter AND setter required for this map
>     private Map<Long, String> answersMap;
> }
> 
> In the ViewQuestionsAction (which sources the JSP):
> 1) loads the questions from the DB;
> 2) populates all the possible answers into the answersList
> 3) populates the map with the current answers, keyed by question id
> 
> When the page loads, the current answer is selected in the radio (due to
> value selecting the answer from the answersMap), and the radios are unique
> by question because the name is unique by question (due to the map keyed
> by question). 
> 
> When you click save, the SaveAnswersAction.answersMap map is populated
> with the selected answers, keyed by the question's id (no prepareable
> method needed). You can then do all the validation you need and update the
> DB as appropriate.
> 
> I believe this is a really nice solution as loading entities from the DB
> in a prepare() method seems wrong to me. I'd be interested to hear what
> other people think. 
> 
> Many thanks
> James
> 
> 
> 
> Rubbinio wrote:
>> 
>> 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.
>> 
>> http://www.vitarara.org/cms/struts_2_cookbook/updating_a_list_of_domain_entities
>> 
>> 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.
>> 
>> HTH
>> Radu
>> 
>> -----Original Message-----
>> From: Jon Pearson [mailto:Jon.Pearson@sixnet.com]
>> 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:
>>> http://www.nabble.com/Radio-button-grouping-in-Struts-2-tp2336
>>> 5502p23365502.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
>>>
>> 
>> 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*
>> functions.
>> 
>> ~Jonathan
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>> 
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Radio-button-grouping-in-Struts-2-tp23365502p23593931.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


RE: Radio button grouping in Struts 2

Posted by "jdt.me.uk" <jd...@gmail.com>.

Hi Radu. Many thanks for your reply, and sorry for not responding sooner as
I've only just got around to looking at your link.

It definitely helped and I've got it working now. It's slightly different
and doesn't rely on Preparable:

class Question
{
    long id;
    String name;
}

public class ViewQuestionsAction
{
    // NOTE - getters required for all these properties
    List<Question> questions;
    List<String> answersList;  
    Map<Long, String> answersMap;
}

jsp:
<s:set name="answersList" value="%{answersList}"/>
<s:set name="answersMap" value="%{answersMap}"/>
<s:iterator value="questions" status="question_stat">  
    <s:property value="name"/><br/>
    <s:radio name="answers[%{id}]" value="%{#answersMap[id]}"
list="#answersList" />
</s:iterator>

public class SaveAnswersAction
{
    // NOTE - getter AND setter required for this map
    private Map<Long, String> answersMap;
}

In the ViewQuestionsAction (which sources the JSP):
1) loads the questions from the DB;
2) populates all the possible answers into the answersList
3) populates the map with the current answers, keyed by question id

When the page loads, the current answer is selected in the radio (due to
value selecting the answer from the answersMap), and the radios are unique
by question because the name is unique by question (due to the map keyed by
question). 

When you click save, the SaveAnswersAction.answersMap map is populated with
the selected answers, keyed by the question's id (no prepareable method
needed). You can then do all the validation you need and update the DB as
appropriate.

I believe this is a really nice solution as loading entities from the DB in
a prepare() method seems wrong to me. I'd be interested to hear what other
people think. 

Many thanks
James



Rubbinio wrote:
> 
> 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.
> 
> http://www.vitarara.org/cms/struts_2_cookbook/updating_a_list_of_domain_entities
> 
> 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.
> 
> HTH
> Radu
> 
> -----Original Message-----
> From: Jon Pearson [mailto:Jon.Pearson@sixnet.com]
> 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:
>> http://www.nabble.com/Radio-button-grouping-in-Struts-2-tp2336
>> 5502p23365502.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
>>
> 
> 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*
> functions.
> 
> ~Jonathan
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Radio-button-grouping-in-Struts-2-tp23365502p23489068.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


RE: Radio button grouping in Struts 2

Posted by Radu Solomon <rs...@n-able.com>.
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.

http://www.vitarara.org/cms/struts_2_cookbook/updating_a_list_of_domain_entities

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.

HTH
Radu

-----Original Message-----
From: Jon Pearson [mailto:Jon.Pearson@sixnet.com]
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:
> http://www.nabble.com/Radio-button-grouping-in-Struts-2-tp2336
> 5502p23365502.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
>

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*
functions.

~Jonathan

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


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


RE: Radio button grouping in Struts 2

Posted by Jon Pearson <Jo...@sixnet.com>.
> 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-tp2336
> 5502p23365502.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
> 

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*
functions.

~Jonathan

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