Solved

Struts Nested radio buttons

Posted on 2006-10-29
11
2,555 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
attribute vs parameter and setter vs add method 17 89
stateless EJB with glassfish 2 73
java cigar challenge 58 214
ejb on wildfly 5 30
Most MSPs worth their salt are already offering cybersecurity to their customers. But cybersecurity as a service is wide encompassing and can mean many things.  So where are MSPs falling in this spectrum?
Learn how to PXE Boot both BIOS & UEFI machines with DHCP Policies and Custom Vendor Classes
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

840 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