Struts 2 Push Tag Example

Struts 2 Push Tag pushes the values to top of the stack. Once the push tag pushes the values, then those fields can be easily accessed without the bean references. Lets see the below example for how to use the push tag.

1. Action Class and Bean

Struts2HelloWorldAction.java

package javabeat.net.struts2;

public class Struts2HelloWorldAction {
	private String userName;

	public String getUserName() {
		return userName;
	}

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

UserDetails.java

package javabeat.net.struts2;

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

}

2. Push Tag Example

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Struts 2 Push Tag Example </title>
	</head>
	<body>
		<s:bean name="javabeat.net.struts2.UserDetails" var="users">
		   <s:param name="name">Hello World</s:param>
		   <s:param name="city">London</s:param>
		</s:bean>
		<s:push value="#users">
			<s:property value="name" /> ,
			<s:property value="city" />
		</s:push>
	</body>
</html>

3. Struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<constant name="struts.devMode" value="true" />
	<package name="tags" extends="struts-default">
		<action name="pushtag" class="javabeat.net.struts2.Struts2HelloWorldAction"
			method="execute">
			<result name="success">/PushTag.jsp</result>
		</action>
	</package>
</struts>

4. Push Tag Demo

If you access the application using the URL http://localhost:8080/Struts2App/pushtag

Struts 2 Push Tag Example Screen

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pin It on Pinterest

Share This

Share this post with your friends!

Share This

Share this post with your friends!