Struts 2 <s:radio> radio button example

Struts 2 <s:radio> radio button example

Previous Home Next

 

The Struts 2.0 framework provide radio button to select only one item in the list through the<s:radio></s:radio> tag. 
This tag provide the facility to get single element in the list .
The Struts 2 radio component provide the following <s:radio></s:radio> tag:

<s:radio list="{'Mail','Female','Unknone'}" label="Gender" name="gender"/>

 
Directory Structure of <s:radio> tag Example in Struts 2.0 Using MyEclipse IDE



index.jsp

<%@taglib uri="/struts-tags" prefix="s"%>

<s:form action="radioEx">
<s:radio list="{'Mail','Female','Unknone'}" label="Gender" name="gender"/>
<s:radio list="{'Book1','Book2','Book3'}" label="Book Name" name="book"/>
<s:submit value="Submit"/>
</s:form>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
  <filter>
  <filter-name>f1</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>f1</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

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>
<package name="demo" extends="struts-default">
<action name="radioEx" class="org.r4r.RadioAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>

RadioAction.java

package org.r4r;

public class RadioAction {
	private String gender;
	private String book;
	
	public String execute(){
		return "success";
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getBook() {
		return book;
	}
	public void setBook(String book) {
		this.book = book;
	}
}

success.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<b>
Gender :-<s:property value="gender"/><br/>
Book Name :-<s:property value="book"/></b>

Output






Previous Home Next