Struts 2 sradio input type radio sample code

Struts 2 sradio input type radio sample code

Here you will see how to use s:radio which is similar to input type radio. Below is sample which we use in our demo and also field label will be retrieved from ResourceBundle using key attribute:

<s:radio list=”radioButtonList” name=”selectedRadionButton” id=”radionButton” label=”label.state” />

  • Project structure:

Struts 2 sradio input type radio sample code

  • input.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Struts 2 sradio input type radio sample code</title>
<s:head />
</head>
 
<body>
	<h3 style="color: green">
		<s:text name="Struts 2 sradio input type radio sample code" />
	</h3>
	<s:form action="struts2SampleCodeAction" namespace="/" method="post"
		name="strutsForm">		
		<s:radio list="radioButtonList" name="selectedRadionButton" id="radionButton" label="label.state" />
		<s:submit/>
	</s:form>
</body>
</html>
  • success.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Struts 2 sradio input type radio sample code</title>
<s:head />
</head>

<body>
	<h2 style="color: green">
		<s:text name="Value entered:" />
	</h2>

	<p>Selected Radio Button value: <s:property value="selectedRadionButton" /></p>	

</body>
</html>
  • Struts2SampleCodeAction.java:
package com.javahonk.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionSupport;

@Results({ @Result(name = "error", location = "/index.jsp"),
		@Result(name = "success", location = "/success.jsp"),
		@Result(name = "input", location = "/input.jsp") })
public class Struts2SampleCodeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String selectedRadionButton;
	private List<String> radioButtonList = new ArrayList<String>();
	

	public Struts2SampleCodeAction() {
		radioButtonList.add("NY");
		radioButtonList.add("NJ");
		radioButtonList.add("MA");
		setRadioButtonList(radioButtonList);		
	}

	@Override
	@Action(value = "/struts2SampleCodeAction")
	public String execute() throws Exception {
		return ActionSupport.SUCCESS;
	}

	@Action(value = "/displayAction")
	public String displayPage() {
		return ActionSupport.INPUT;
	}	

	public List<String> getRadioButtonList() {
		return radioButtonList;
	}

	public void setRadioButtonList(List<String> radioButtonList) {
		this.radioButtonList = radioButtonList;
	}

	public String getSelectedRadionButton() {
		return selectedRadionButton;
	}

	public void setSelectedRadionButton(String selectedRadionButton) {
		this.selectedRadionButton = selectedRadionButton;
	}
	
}
  • Web page output:

Struts 2 sradio input type radio sample code

  • After Submit button click:

Struts 2 sradio input type radio sample code

For more information please visit struts site here

download  Download Project:  Struts2ExampleCode

 

 

 

Leave a Reply

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