Pages Navigation Menu

Coding is much easier than you think

Struts 2 Push Tag Example

Struts 2 Push Tag Example

Struts 2 “push” tag is used to push a value into the ValueStack. The value we pushed using push tag will be on top of the ValueStack, so it can be easily referenced using the first-level OGNL expression instead of a deeper reference. The following code show how to do this.
** UPDATE: Struts 2 Complete tutorial now available here.

Download It – PushTag

 1. Action

package com.simplecode.action;

import com.simplecode.util.AuthorBean;

//PushTag Action class
public class BookInfoAction {

	private AuthorBean authorBean;

	public String populate() {
	  authorBean = new AuthorBean
             ("Mohammed masjid", "Akuland Nz","8051 Micro Controller");
		return "populate";

	public String execute() {
		return "success";

	public AuthorBean getAuthorBean() {
		return authorBean;

	public void setAuthorBean(AuthorBean authorBean) {
		this.authorBean = authorBean;


 2. Bean
A simple AuthorBean class, later will push it into the stack for easy access.

package com.simplecode.util;

public class AuthorBean {

	private String name;
	private String university;
	private String book;

	public AuthorBean(String name, String university, String book) { = name; = university; = book;

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public String getUniversity() {
		return university;

	public void setUniversity(String university) { = university;

	public String getBook() {
		return book;

	public void setBook(String book) { = book;

 3. Push tag example
It shows the use of “push” tag.

<%@taglib uri="/struts-tags" prefix="s"%>
<title>Book Details</title>
	<p>Author name property can be accessed in two ways:</p>
	<br /> (Method 1 - Normal method) 
	<br /> <b>Author Name:</b>
	<s:property value="" />
	<br /> <b> University: </b>
	<s:property value="" />
	<br/><b>Book Title :</b>
	<s:property value="" />
	<br /> <br /> (Method 2-push tag) 
	<s:push value="authorBean">
	   <b>Author Name:</b>
	   <s:property value="name" />
	   <b> University: </b>
	   <s:property value="university" />
	   <b>Book Title :</b>
	   <s:property value="book" />


 How it work?
Normally, if you want to get the bean’s property, you may reference it like

<s:property value="" />

With “push” tag, you can push the “authorBean” to the top of the stack, and access the property directly

<s:property value="name" />

Both are returned the same result, but with different access mechanism only.

 4. Struts.xml
Link it

<?xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
	<package name="default" extends="struts-default">	
		<action name="*BookAction" method="{1}"
			<result name="populate">/bookDetails.jsp</result>

 5. Demo
 Run it

About Mohaideen Jamil

I'm a Full stack developer of IT exp in J2EE, AngularJs, MicroServices, Docker, Spring (Boot, MVC, Cloud), Bluemix, DevOps. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

%d bloggers like this: