• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Struts 2 Radio Tag Example

December 14, 2013 //  by Krishna Srinivasan//  Leave a Comment

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

package javabeat.net.struts2;

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>();
			cities.add("London");
			cities.add("Newyork");
			cities.add("Tokyo");
			return "INDEX";
		}
		return "success";
	}
}

2. Radio Tag Example

RadioButton.jsp

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

Result.jsp

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

3. Struts.xml

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

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

Category: StrutsTag: Struts 2 Tutorials

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.

Previous Post: « Struts 2 CheckBox Tag Example
Next Post: Struts 2 Select Tag Example »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact