Reputation: 502
I am making a survey web app. For each question, I have like this:
Question: this is a question. Please select the best choice.
*The option list is dynamic.
And I am using Struts2 radio
So in my Java action class, I have
question.addOptions("This is option 1.");
question.addOptions("This is option 2.");
question.addOptions("This is option 3.");
And in my jsp, I have
<s:radio label="Please select the best choice" name="answer" list="options"/>
where "answer" is an attribute(instance variable) of my action class, to record the selection.
So right now as I select one of the options, I can see "answer" record the value as "This is Option x". Not bad.
However, here is what I want to achieve. I want "answer" to value "x" instead of "This is Option x." But I have really not found a way to customize this part.
Upvotes: 1
Views: 1793
Reputation: 13716
1) Solution using JSP Change
Here value of x is 1 or 2 or 3 or 4. Instead of "This is Option x"
<s:radio label="YourQuestion" name="yourAnswer" list="#{'1':' This is Option 1','2':' This is Option 2','3':' This is Option 3','4':' This is Option 4'}" value="1" />
value attribute will set the default value. list will contain the radio button values.
2) EDIT: Another Solution Using Java
For Dynamic Options create something like this
public class RadioOptions{
private String key;
private String value;
//getter and setter methods
}
Here you can retrieve the calls using the key value.
List<RadioOptions> radioOption= new ArrayList<RadioOptions>();
radioOption.add( new RadioOptions("1", "This is option 1.") );
radioOption.add( new RadioOptions("2", "This is option 2.") );
radioOption.add( new RadioOptions("3", "This is option 3.") );
radioOption.add( new RadioOptions("4", "This is option 4.") );
Your HTML Tag look like this.
<s:radio label="Please select the best choice" name="answer" list="radioOption"
listKey="key" listValue="value" value="1" />
Upvotes: 3