Struts 2 <s:radio> radio button example
In Struts 2 , you can use the <s:radio> tag to create a HTML radio button. The funny stuff is there are many ways to populate the data into the radio button, via List, OGNL or Object. Check the below example to know how.
Struts 2 <s:radio> example
Example to show the use of List, OGNL and Object to populate data to a radio button that’s created via Struts 2 <s:radio> tag.
1. Action
Action class to create list of the genders and languages values for radio button.
RadioButtonAction.java
package com.mkyong.common.action; import java.util.ArrayList; import java.util.List; import com.mkyong.common.model.Language; import com.opensymphony.xwork2.ActionSupport; public class RadioButtonAction extends ActionSupport{ private List<String> genders; private List<Language> languages ; private String yourGender; private String yourAnswer; private String yourLanguage; private static final String MALE = "male"; private static final String FEMALE = "female"; private static final String UNKNOWN = "unknown"; public RadioButtonAction(){ genders = new ArrayList<String>(); genders.add(MALE); genders.add(FEMALE); genders.add(UNKNOWN); languages = new ArrayList<Language>(); languages.add( new Language("EN", "English") ); languages.add( new Language("FR", "France") ); languages.add( new Language("CN_ZH", "Chinese") ); languages.add( new Language("DE", "German") ); } //return default gender value public String getDefaultGenderValue(){ return UNKNOWN; } //return default language value public String getDefaultLanguageValue(){ return "CN_ZH"; } public String execute() { return SUCCESS; } public String display() { return NONE; } //getter ad setter methods }
Language.java
package com.mkyong.common.model; public class Language{ private String languageCode; private String languageDisplay; //getter and setter methods public Language(String languageCode, String languageDisplay) { this.languageCode = languageCode; this.languageDisplay = languageDisplay; } }
2. Result page
Result page to use Struts 2 “s:radio” to create three radio buttons via List, OGNL and object.
radiobutton.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 radio button example</h1> <s:form action="resultAction" namespace="/"> <h4> <s:radio label="Gender" name="yourGender" list="genders" value="defaultGenderValue" /> <s:radio label="Gender" name="yourLanguage" list="languages" listKey="languageCode" listValue="languageDisplay" value="defaultLanguageValue" /> <s:radio label="Answer" name="yourAnswer" list="#{'1':'Yes','2':'No'}" value="2" /> </h4> <s:submit value="submit" name="submit" /> </s:form> </body> </html>a
result.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <body> <h1>Struts 2 radio button example</h1> <h4> Your Selected Gender : <s:property value="yourGender"/> </h4> <h4> Your Selected Language : <s:property value="yourLanguage"/> </h4> <h4> Your Selected Answer : <s:property value="yourAnswer"/> </h4> </body> </html>
3. struts.xml
Link all together ~
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="" namespace="/" extends="struts-default"> <action name="radioButtonAction" class="com.mkyong.common.action.RadioButtonAction" method="display"> <result name="none">pages/radiobutton.jsp</result> </action> <action name="resultAction" class="com.mkyong.common.action.RadioButtonAction"> <result name="success">pages/result.jsp</result> </action> </package> </struts>
5. Demo
http://localhost:8080/Struts2Example/radioButtonAction.action

http://localhost:8080/Struts2Example/resultAction.action

Reference
- http://struts.apache.org/2.0.11.2/docs/radio.html
- http://struts.apache.org/2.1.8.1/docs/struts-2-form-tags.html
- How to preselect a radio button value in Struts 2







in struts.xml….action name=”radioButtonAction”..frm where dis “radioButtonAction” is called ?
Dear MKyong!
How can get value key of list Language ?
Exampe: i want to get value key of English is “EN” or get value key of France is “FR”
Thank so much
Hi,
I don’t understand the flow of the above program of radio button, i mean how the values get displayed in radiobutton.jsp before calling the RadioButtonAction and when the constructor of action is called..
Thanks a lot for providing this type of tutorials.
what are the jar files required.
This is Maven project, bro. the pom.xml file is included in the attachment zip file.
Hi,
Thanks for your example of how to use radio button.
I have few other text fields in the jsp, and I am using action-validation.xml to validate those text fields.
Upon validation error, directed to jsp.
Now jsp cant find list and throwing errors.
Can you help me to solve this problem.
Thanks and appreciate your help
I believe no one can answer by just view the statement above :). Zip your project and send it to me, Maven project prefer.
P.S Do not send me all the included libraries, as it will caused my mail server goes down TT…
[...] Create a radio button in Struts 2 VN:F [1.9.1_1087]please wait…Rating: 0.0/10 (0 votes cast) [...]