Struts 2 Bean Tag Example

Struts 2 Bean Tag is helpful for assigning a bean reference to the JSP page. Using the s:param attribute, you can pass the values to the bean reference and set the values. After this tag is executed, you can use the bean instance throughout the page.

1. Action Class and Bean


public class Struts2HelloWorldAction {
	private String userName;

	public String getUserName() {
		return userName;

	public void setUserName(String userName) {
		this.userName = userName;
	public String execute(){
		return "success";


public class UserDetails {
	private String name;
	private String city;
	public UserDetails(){}
	public UserDetails(String... args){ = args[0]; = args[1];
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public String getCity() {
		return city;
	public void setCity(String city) { = city;


2. Bean Tag Example

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Struts 2 Bean Tag Example </title>
<s:bean name="" var="users">
   <s:param name="name">Rahul</s:param>
   <s:param name="city">Chennai</s:param>

<s:property value="" /> ,
<s:property value="" />


3. Struts.xml

<?xml version="1.0" encoding="UTF-8"?>
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
	<constant name="struts.devMode" value="true" />
	<package name="tags" extends="struts-default">
		<action name="beantag" class=""
			<result name="success">/BeanTag.jsp</result>

4. Bean Tag Demo

Run the bean tag example by accessing the URL http://localhost:8080/Struts2App/beantag.

Struts 2 Bean Tag Example 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.