Solved

Struts Nested radio buttons

Posted on 2006-10-29
11
2,537 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
Comment Utility
>>>  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
Comment Utility
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
Comment Utility
so what error are you getting ?
0
 

Author Comment

by:cool_baba
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:cool_baba
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
How would that make a difference when the radio button is attached to selectedAnswer ..

0
 
LVL 10

Expert Comment

by:jaggernat
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article, I will show you HOW TO: Create your first Windows Virtual Machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, the Windows OS we will install is Windows Server 2016.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now