Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Create Action Class
2.1.
Create a file with the name
2.2.
Source Code
3.
Create Bean Class
3.1.
Create a file with the name
3.2.
Source Code
4.
Create Push Class
4.1.
Create a file with the name
4.2.
Source Code
4.3.
Working
5.
Create an XML file
5.1.
Create file with name
5.2.
Source Code
6.
Output
7.
Frequently Asked Questions
7.1.
What do strut tags mean?
7.2.
In struts, what is form bean?
7.3.
Describe Struts XML.
7.4.
What does the action struts2 mean?
7.5.
What function does .TLD file provides struts?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

The Push Tag

Introduction

This article will demonstrate how to use the push tag of the struts2 framework. The push tag adds value to the top of the stack. As a result, it is accessible with ease by utilizing OGNL expression language at the first level.

introduction

In struts2, the value is set and kept at the top of the stack using the push tag. It makes pushing and accessing the deal simpler.

Create Action Class

Action class for merely forwarding the request.

Create a file with the name

PushingAction.java

Source Code


package com.codingninja.action;
import com.opensymphony.xwork2.ActionSupport;
	public class PushingAction extends ActionSupport{
	public String execute() throws Exception {
		return SUCCESS;
	}
}

Create Bean Class

A straightforward Person class will be afterward pushed into the stack for easier access.

Create a file with the name

DataOfPerson.java

Source Code

package com.codingninja;
public class DataOfPerson{
	private String name = "This is Name";
	private String title = "This is Title";
	public String getName() {
	return name;
	}
	public String getTitle() {
		return title;
	}
}

Create Push Class

The "push" tag is used, as seen.

Create a file with the name

Push.java

Source Code

<html>
<body>
   	 <h2>1. Method</h2>
		<s:bean name="com.codingninja.Person" var="persondataBean" />
			Name : <s:property value="#persondataBean.name" /><br/>
			Title: <s:property value="#persondataBean.title" /><br/>
   	 <h2>2. Push way</h2>
	<s:push value="#persondataBean" >
			Name : <s:property value="name" /><br/>
			Title: <s:property value="title" /><br/>
	</s:push>
</body>
</html>

Working

working

In most cases, you can reference a bean's property using something like s: property value="#persondataBean.name" />. You can directly access the property with the "push" tag by moving the "#persondataBean" to the top of the stack and setting the value of the property to "name." The identical outcome is returned to both, but merely through a different access method.

You can avoid typing a few letters using the "push" tag, but there isn't much of a benefit.

Create an XML file

We have to link this file.

xml

Create file with name

struts.xml

Source Code

<?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="default" namespace="/" extends="struts-default">

		<action name="pushTagAction" 
			class="com.codingninjas.action.PushTagAction" >
			<result name="success">pages/push.jsp</result>
		</action>

			</package>
	</struts>

Output

output

Frequently Asked Questions

What do strut tags mean?

The Struts HTML Tags drawer allows you to build Struts HTML tags. These tags, along with others that are usually helpful in the HTML-based user interfaces, are used to create Struts input forms. When in XHTML mode, the output is XHTML 1.0 compliant or HTML 4.01 compliant.

In struts, what is form bean?

A Java bean, known as a form bean, is filled with the information from an HTML form created using Struts when a user presses the Submit button on that form. A form bean's characteristics correspond to every field on the form. The bean properties are immediately filled in after the form is submitted.

Describe Struts XML.

The configuration data you will change when actions are created is contained in the struts.xml file. DevMode = false, and other default options given in property files can be overridden for an application using this file, such as struts.

What does the action struts2 mean?

Advertisements. Programmers can use the action tag to carry out the action from the view page. By naming the activity, they can achieve this. To render the result in the view directly, they can set the "executeResult" option to "true."

What function does .TLD file provides struts?

To store a mapping between the TLD and its URI, the servlet container searches for the uri> element inside the root element taglib> while loading the TLD. In yours. JSP files, the TLD is accordingly referenced using this URI.

Conclusion

This article has gone through push tag in Struts. For example, if you want to learn more about frameworks, please click here. I suppose it is clear to you. Still, have doubt? Then please comment.

You may also CheckOut our other article on Spring Boot.

If you find any problems, please comment. We will love to answer your questions.

Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Ninja, have a great time learning.

Thank You

Live masterclass