Struts 2 Radio Tag Example

Struts 2 s:radio button tag helps you to select only one value from the list of options. Unlike check boxes which offer multiple selection possible, radio buttons are useful for selecting only one value from the group of options. This radio button tag example provides very simple demonstration. Lets look at these code snippets.

1. Action Class


import java.util.ArrayList;

public class Struts2UITagsAction{
	private String cityName;
	private ArrayList<String> cities;
	public String getCityName() {
		return cityName;
	public void setCityName(String cityName) {
		this.cityName = cityName;
	public ArrayList<String> getCities() {
		return cities;
	public void setCities(ArrayList<String> cities) {
		this.cities = cities;
	public String execute(){
		if (cityName == null || cityName.trim().length() == 0){
			cities = new ArrayList<String>();
			return "INDEX";
		return "success";

2. Radio Tag Example


<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
	<h1>JavaBeat - Struts 2 Radio Button Tag Demo</h1>
	<s:form action="RadioDemo">
		<s:radio name="cityName" list="cities"/>
		<s:submit label="Submit" />


<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
	<h1>JavaBeat - Struts 2 Radio Button Tag Demo</h1>
		Selected City :	<s:property value="cityName" /><br>

3. Struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 2.1//EN" "">
<constant name="struts.devMode" value="true" />
<package name="textfielddemo" extends="struts-default">
	<action name="RadioDemo" class="" >
		<result name="success">Result.jsp</result>
		<result name="INDEX">RadioButton.jsp</result>

4. Radio Tag Demo

If you access the application using URL http://localhost:8080/Struts2App/RadioDemo, you would see the following output.

struts2 radio tag example input screen

struts2 radio tag example output screen



About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

  • Pooja

    Hi Krishna,
    I am getting the following error when I run the code, even though I have taken cities as an ArrayList of Strings.
    org.apache.jasper.JasperException: tag ‘radio’, field ‘list’, name ‘cityName': The requested list key ‘cities’ could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} – [unknown location]