Link to home
Start Free TrialLog in
Avatar of cool_baba
cool_baba

asked on

Struts Nested radio buttons

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
     
Avatar of jaggernat
jaggernat

>>>  value='<%=""+ind%>'    

what is ind?

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

ASKER

I forgot to add indexId in logic:iterate
<logic:iterate id="anAnswer" name="aGroup" property="answers" indexId="ind">
so what error are you getting ?
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.
 
 
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>
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
    ...
 ...

 
ASKER CERTIFIED SOLUTION
Avatar of jaggernat
jaggernat

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
 
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.





How would that make a difference when the radio button is attached to selectedAnswer ..

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.