Loading list of radio buttons- Struts
Hello All, i have a question based on this. How can I load a list of radio buttons instead of list of select values.
For eg., I have 5 questions and each question has 2 answers (yes/no). I iterate 5 questions coming from database. Now i need to generate 2 radio buttons (yes/no)for each question. The values for these radio buttons is also coming fom database.
I have sample code for generating list of drop downs. Can anyone help me with generating list of radio buttons. This is the sample code:
<html:select name="formBeanName" property="beanproperty" styleClass="formElementSelect" indexed="true">
<html:optionsCollection name="formBeanName" property="List" label="textValue" value="id" />
</html:select>
Thanks,
Sai
You can create an object for each such question radio options
The object should have questionText,yesLabel,noLabel, yesValue,NoValue, value(representing real selected radio value) getter/setters.
Make a collection named "questionRadioOptions" in the form and iterate thru this collection in jsp
can use logic tag instead of nested tags
<nested:iterate property="questionRadioOptions"name="yourForm">
<nested:write property="questionText"/>
<nested:notEmpty property="yesLabel">
<nested:define id="yesValue" property="yesValue" type="java.lang.String"/><nested:radio value='<%=yesValue%>'property="value"/> <nested:write property="yesLabel"/>
</nested:notEmpty>
<nested:notEmpty property="noLabel">
<nested:define id="noValue" property="noValue" type="java.lang.String"/><nested:radio value='<%=noValue%>'property="value"/> <nested:write property="noLabel"/>
</nested:notEmpty>
</nested:iterate>
Thank you for the solution, but looks like its not working. I dont know what i am doing wrong.
My form bean for example looks something like this.
<BEAN name="A">
<PROPERTY name="property1" type="java.util.List" containedType="B"/>
</BEAN>
<BEAN name="B">
<PROPERTY name="property2" type="java.lang.String"/>
<PROPERTY name="list" type="java.util.List" containedType="C"/>
</BEAN>
<BEAN name="C">
<PROPERTY name="id" type="java.lang.String" />distinct value for 'Yes' and 'No' from database
<PROPERTY name="textValue" type="String" />-Yes/No
</BEAN>
All the above properties have getter and setter methods. The "list" property is what u mentioned as a collection. list.id and list.textValue are available to me as they are coming from database. I need to assign the selection of Yes/No to "property2" of the form A.
Could you pls help me in this regard.
Thank you in advance.
Sai
Could someone help me with this questions ?ThanksSai
More explicit....
Object RadioOptions
{String yesLabel,noLabel,yesValue,noValue;
String value;
//getter/setters
}
In Action form
{ArrayList questionRadioOptions;
//besides normal getter/setters
public void setQuestionRadioOptions(int index, RadioOptions radioOptions)
{questionRadioOptions.set(index, radioOptions);}
public RadioOptions getQuestionRadioOptions(String index)
{return (RadioOptions) questionRadioOptions.get(Integer.parseInt(index));}
}
In Action class //I am assuming you are getting data from databse here
//Fetch the data and create/populate object RadioOptions
//If you want to prepopulate the radio option with say Yes value then
radioOptions.setValue(databasevalue);
//add this object in an arraylist when all data has been fetched set it in form
form.setQuestionRadioOptions(arrayList) ;
Follow the jsp as in previous reply.Value is an important attribute here.
Ash
Thank you once again for the approach. There has been some progress now but still i am not getting the desired result. I have already created RadioOptionsList.
So rt now i have a collection. And i am using logic:iterate tag to iterate through this collection to create a list of radio buttons (in my case 2 radio buttons(Yes & No) ) for each question. It is creating 2 radio buttons Yes & No for me for each question. But the problm is that these 2 radio buttons are not in a group. In the sense that i am able to select both the Yes and No radio buttons for a single question. And if i select say radio button Yes for quesiton 1........And i go to the next question and select Yes button...........the Yes radio button of the first question gets deselected. I hope i am not confusing u..
Could u pls explain what i am missing here ...
Thanks in advance.
Sai.
I had this problem...
I think you are creating radiooption for each such yes and no is it?If yes then that should not be the case.It should rather be...
One RadioOption object should represent both yes as well as no then only you will get the following radio option names in html view source
radioOption[1].yes for first set
radioOption[1].no
radioOption[2].yes for second set
radioOption[2].no
Ash
Yes what u understood is true. I am getting the result as u said.
radioOption[0].yes --for first set
radioOption[1].no
radioOption[0].yes --for second set
radioOption[1].no
I need to get the result like u said.How do i do that ?
My FormBean is like this :
<BEAN name="AForm">
<PROPERTY name="a" type="java.util.List" access="get,set" containedType="B"/>
</BEAN>
<BEAN name="B">
<PROPERTY name="radioOptions" type="java.util.List" access="get,set" containedType="C"/>
<PROPERTY name="b" type="java.lang.String" access="get,set" />
</BEAN>
<BEAN name="C">
<PROPERTY name="id" type="java.lang.String" access="get,set" />
<PROPERTY name="textValue" type="String" access="get,set" />
</BEAN>
This is how i am iterating through the radioOptions Collection within another iteration for the questions generation in the jsp.
<logic:iterate id="b" property="radioOptions" name="a" >
<html:radio name = "a" property="b" value = "id" indexed="true" idName="b"/>
<bean:write name="b" property = "textValue"/>
</logic:iterate>
Could u pls let me know where i am doing wrong in the logic:iterate
Thank You in advance,
Sai.
sai,
your bean C needs one more set of getter and setter to store noValues./text
<BEAN name="C">
<PROPERTY name="idYes" type="java.lang.String" access="get,set" />
<PROPERTY name="yesTextValue" type="String" access="get,set" />
<PROPERTY name="idNo" type="java.lang.String" access="get,set" />
<PROPERTY name="noTextValue" type="String" access="get,set" />
</BEAN>
When you populate that bean if it is 'Yes' then set yes fields else set 'No' fields.
then in jsp you would do..
I used nested tags which are convinient than logic tags..You can use it too.
I am not sure if this syntax is correct...for logic
<logic:iterate property="a" name="AForm" >
<bean:define id="b" property="b" type="java.util.List"/>
<bean:define id="yesValue" property="idYes" name="b"/>
<html:radio idName="b" property="idYes" value = "<%=yesValue%>" />
<bean:write idName="b" property = "yesTextValue"/>
//this will render radio as <input type="radio" name="a[0].value" value="yes">yes
//for no
<bean:define id="noValue" property="idNo" name="b"/>
<html:radio idName="b" property="idNo" value = "<%=noValue%>" />
<bean:write idName="b" property = "noTextValue"/>
//this will render radio as <input type="radio" name="a[0].value" value="no">No
</logic:iterate>
for nested it would be
<nested:iterate property="a" name="AForm">
//generate radio option for yes
<nested:define id="idYesValue" property="idYes" type="java.lang.String"/>
<nested:radio value='<%=idYesValue%>' property="value"/>
<nested:write property="yesTextValue"/>
//generate radio option for no
<nested:define id="idNoValue" property="idNo" type="java.lang.String"/>
<nested:radio value='<%=idNoValue%>' property="value"/>
<nested:write property="noTextValue"/>
</nested:iterate>
Ash
Thank you once again. But i used the same bean properties as i showed in my last message and i could popluate the drop down with the Yes/No values for each question.
In my form "C",
property 'id' comes from database and it has distinct values for 'Yes' &'No' textValue property.
Eg., id = 22 -textValue="No"
id = 23 -textValue = "Yes"
id = 24 -textValue="No"
id = 25 -textValue = "Yes".......so on.
So i need only one "id"& "textValue" property. So is there a way that i could use only 1 "id" & "textValue" properties and generte what i want. Is there a way to index this.
Thanks a lot again
Sai.
I used the following code to populate the drop down list. I forgot to put this in the prev message. So thought this might help ....
<html:select name="a" property="b" styleClass="formElementSelect" indexed="true">
<html:optionsCollection name="a" property="selectOptionsList" label="textValue" value="id" />
</html:select>
Formbean properties and names correspond to my prev message
Snce radio buttons work in groups .Only when they have one uniques ID both for yes and No then can you select any one value.Iam afraid you will haveto add the get/set as I suggested.
Ya i know that radio buttons work in groups but even in the case of html:select we can only select 1 item at a time rt (atleast in my case thats true). So can we not use my formBean properties.Thank You,Sai.