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.pear...@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-unsubscr...@struts.apache.org >>> For additional commands, e-mail: user-h...@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-unsubscr...@struts.apache.org >> For additional commands, e-mail: user-h...@struts.apache.org >> >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: user-unsubscr...@struts.apache.org >> For additional commands, e-mail: user-h...@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-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org