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

Struts2 radio button screen

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

Struts2 radio button screen
To understand how to preselect a value, read this article – preselect a radio button value in Struts 2

Reference

  1. http://struts.apache.org/2.0.11.2/docs/radio.html
  2. http://struts.apache.org/2.1.8.1/docs/struts-2-form-tags.html
  3. How to preselect a radio button value in Struts 2
Tags :
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

Related Posts

Popular Posts