Solved

Struts Nested radio buttons

Posted on 2006-10-29
11
2,550 Views
Last Modified: 2008-04-27
Hi Experts,
       I am working on mulitple choice questions and using struts. main form class has an array of Group of questions.
   
 MultipleChoiceFrom.java :
     Group[] questionAnswerGroup;


 Group.java :
   
     String question;
     int selectedAnswer;  // used for radio button.
     Answers[] answers;


 Answer.java :
    // other parameters
     String answer;

   now in JSP page
 

  Now in action I load data from database .. but dont know how to implement it in JSP .. I am using nested logic:iterate

   <logic:iterate id="aGroup" name="multipleChoiceForm" property="questionAnswerGroup" indexId="index">
         <tr>
      <td bgcolor="#c0c0c0" colspan="3" align="center" width="100%" height="30px">
                  <bean:write name="aGroup" property="question" />
      </td>
        </tr>
        <tr>
      <td valign="top">
             <table>
            <logic:iterate id="anAnswer" name="aGroup" property="answers" >
                   <tr>
                  <td>
                           <html:radio name="aGroup" property="selectedAnswer" value='<%=""+ind%>' />                                             <bean:write name="anAnswer" property="answer" />
                            </td>
                  </tr>
            </logic:iterate>
             </table>
      </td>
        </tr>
</logic:iterate>

    It let me select just one radio button out of all radio buttons on page .. Please suggest me a solution ..
thanks
 Cool
     
0
Comment
Question by:cool_baba
  • 6
  • 5
11 Comments
 
LVL 10

Expert Comment

by:jaggernat
ID: 17829402
>>>  value='<%=""+ind%>'    

what is ind?

If you want users to select multiple answers, use checkbox instead of radio buttons.
0
 

Author Comment

by:cool_baba
ID: 17829458
I forgot to add indexId in logic:iterate
<logic:iterate id="anAnswer" name="aGroup" property="answers" indexId="ind">
0
 
LVL 10

Expert Comment

by:jaggernat
ID: 17829481
so what error are you getting ?
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:cool_baba
ID: 17829512
all radio buttons on page have same name i.e. selectedAnswer, that cause problem that i can select only one radio button on whole page.
  I want user to select one radio button for each question but it allows me to select one radio button for all questions.
 
 
0
 
LVL 10

Expert Comment

by:jaggernat
ID: 17829545
try this


<logic:iterate id="aGroup" name="multipleChoiceForm" property="questionAnswerGroup" indexId="index">
         <tr>
     <td bgcolor="#c0c0c0" colspan="3" align="center" width="100%" height="30px">
                 <bean:write name="aGroup" property="question" />
     </td>
        </tr>
</logic:iterate>
        <tr>
     <td valign="top">
            <table>
          <logic:iterate id="anAnswer" name="aGroup" property="answers" >
                 <tr>
               <td>
                        <html:radio name="aGroup" property="selectedAnswer" value='<%=""+ind%>' />                                       <bean:write name="anAnswer" property="answer" />
                          </td>
                </tr>
          </logic:iterate>
            </table>
     </td>
        </tr>


//in above code i moved the closing</logic:iterate> tags after finishing first </tr>
0
 

Author Comment

by:cool_baba
ID: 17829562
Well dont you think that it will display all list of questions first and then all list of answers ?
I want it in following format

  Q1 .Question
     (radiobutton) option 1
     (radiobutton)option 2
     (radiobutton)option 3
  Q2  .Question
     (radiobutton) Option1
    (radiobutton) option2
    ...
 ...

 
0
 
LVL 10

Accepted Solution

by:
jaggernat earned 250 total points
ID: 17829603
take a look at this sample. its doing pretty much what you are trying to do

http://www-128.ibm.com/developerworks/java/library/j-sr4.html

0
 

Author Comment

by:cool_baba
ID: 17829645
Yes its doing exactly what i am doing in inner logic:iterate loop. but when i add an additional level of iteration that is having more than one questions .. or more than one list of mountains in your sample code then problem occurs that it doesnt let me select one radio button from each question.
 
0
 
LVL 10

Expert Comment

by:jaggernat
ID: 17829726
You are storing your answers in this array   Answers[] answers;  

What you can do is instead of using   Answers[] answers;

use an ArrayList  like   ArrayList answers;

which would contain different answer objects for each question
Then, Iterate the arraylist (instead of array) and display your radio buttons.





0
 

Author Comment

by:cool_baba
ID: 17838134
How would that make a difference when the radio button is attached to selectedAnswer ..

0
 
LVL 10

Expert Comment

by:jaggernat
ID: 17838483
Array is just single holder of Answer object. that is why you are able to select only one radion button. On the other hand, an arraylist is a collection of objects. so in your case , it would be a collection of Answer objects.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
J2SE 5.0 metadata 1 56
servlet init param and contect param 3 64
own marker interface 1 54
listing all functions in JavaScript 19 200
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Data breaches are on the rise, and companies are preparing by boosting their cybersecurity budgets. According to the Cybersecurity Market Report (http://www.cybersecurityventures.com/cybersecurity-market-report), worldwide spending on cybersecurity …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question