Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Struts Nested radio buttons

Posted on 2006-10-29
11
Medium Priority
?
2,607 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Sometimes Administrators rights are not enough. These cases call for the SYSTEM account. The process in this article outlines the steps required to execute commands using the SYSTEM account.
Working from home is a dream for many people who aren’t happy about getting up early, going to the office, and spending long hours at work. There are lots of benefits of remote work for employees.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

618 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