Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
The Property Tag
2.1.
Create Classes
2.2.
Create Views
3.
Configuration Files
3.1.
Output
4.
Frequently Asked Questions
4.1.
What exactly is Struts 2?
4.2.
What are the uses of the Struts 2 framework?
4.3.
Write some Common Data tags? 
4.4.
What is the Property Tag?
4.5.
How can you produce a WAR file?
5.
Conclusion 
Last Updated: Mar 27, 2024
Easy

The Property Tag

Introduction

In this article, we will go through the Property Tag. The property tag is a generic tag that is used to retrieve a value's property, which defaults to the top of the stack if none is supplied.

The property tag is one of the struts2 data tags, the data tags in Struts 2 are mostly used to manipulate the data presented on a page. Some common data tags are the Action Tag, the Include Tag, the Bean Tag, the Date Tag, the Param Tag, the Property Tag, the Text Tag, and the URL Tag. In this blog, we will be talking particularly about the Property tag.

Struct2 Data tags

The Property Tag

The property tag is used to get a value's property, which defaults to the top of the stack if none is given. This example demonstrates using three simple data tags: set, push, and property. The property tag setting does not affect any of the characteristics of an object.

This property tag may be used to assign an identifier string to an object without altering any of its different property settings or causing any other side effects. When you need to verify the identification of a form, report, data access page, section, or control that is supplied as a variable to a method, the Tag attribute comes in handy.

the property tag

Create Classes

Let us re-use examples from the "Data Type Conversion" chapter with minor adjustments for this exercise. So let us begin by taking lessons. 

Consider the POJO class Ninja.java below.

Package com.CodingNinjas.struts2;
public class Ninjas {
   private String name;
   public  Ninjas(String name) 
   {
      this.name = name;
   }   
   public String getName() 
   {
      return name;
   }   
   public void setName(String name) 
   {
      this.name = name;
   }
}

 

Let us consider the following action class NinjaDetails.java :

Package com.CodingNinjas.struts2;
import com.opensymphony.xwork2.ActionSupport;

public class NinjaDetails extends ActionSupport {
   private Ninja ninja = new ninja ("Musical Matrix");
   private String opSystem = "Windows 11";

   public String execute() 
   {
      return SUCCESS;
   }
   
   public Ninja getNinja() 
   {
      return ninja;
   }
   
   public void setNinja(Ninja ninja) 
   {
      this.ninja = ninja;
   }
   
   public String getOpSystem() 
   {
      return opSystem;
   }
   
   public void setOpSystem(String opSystem) 
   {
      this.opSystem = opSystem;
   }
}
Creating Classes for Property Tags

Create Views

Allow us to use Ninjas.jsp with the following content:

<%@ 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"
>
<html>
   <head>
      <title>Ninja Details</title>
   </head>
   
   <body>      
      <p>The ninja name property can be accessed in three ways:</p>

      (Method 1) Ninja Name: 
      <s:property value = "ninja.name"/><br/>

      (Method 2) Ninja Name: 
      <s:push value = "ninja">
         <s:property value = "name"/><br/>
      </s:push>

      (Method 3) Ninja Name:
      <s:set name = "SpeedNinja" value = "ninja.name"/>
      <s:property value = "SpeedNinja"/>
      
   </body>
</html>

 

Let us now go through each Method mention above one by one.

  • The property tag is used in the first method to obtain the value of the Ninja name. Because the Ninjas variable is in the action class, it is available in the value stack by default. We may refer to it directly by using the property ninja.name. Method 1 is sufficient when the number of properties in a class is restricted. Consider having 15 attributes in the Ninja class. Every time you need to refer to these variables, use the prefix "ninja." The push tag comes in helpful at this point.
  • The "ninja" attribute is pushed to the stack in the second approach. As a result, the ninja attribute is now available at the root of the stack within the body of the push tag. As illustrated in the example, you can now quickly refer to the property.
  • The set tag is used in the last procedure to create a new variable named SpeedNinja. The value of this variable is provided to ninja.name. So, whenever we refer to the name of the ninja, we can now utilize this variable.

Configuration Files

Your struts.xml file should look like this:

<?xml version = "1.0" Encoding = "UTF-8"?>
<!doctype struts2 public
   "-//Apache Software Foundation//DTD Struts2.0//EN"
   "http://struts.apache.org/dtds/struts2.0.dtd">
<struts>
   <constant name = "struts.devMode" value = "true" />
   <package name = "helloninjas" extends = "struts-default">
      <action name = "ninja" 
         class = "com.CodingNinjas.struts2.Ninja" 
         method = "execute">
         <result name = "success">/Ninja.jsp</result>
      </action>
   </package>
</struts>

 

Your web.xml file should look like this:

<?xml version = "1.0" Encoding = "UTF-8"?>
<id = "WebApp_ID" version = "3.0">
   <display-name>Struts 02</display-name>
   
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
   
   <filter>
      <filter-name>struts02</filter-name>
      <filter-class>
         org.apache.struts02.dispatcher.FilterDispatcher
      </filter-class>
   </filter>

   <filter-mapping>
      <filter-name>struts02</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
</web-app>

 

To produce a War file:

  1. Right-click on the project name and select Export > WAR File.
  2. After that, place this WAR in Tomcat's web apps directory. Tomcat is a free and open-source Java servlet container that supports various Java Enterprise Specs, including the Websites API, Java-Server Pages, and, of course, the Java Servlet. Tomcat's full name is "Apache Tomcat". 
  3. Finally, launch Tomcat and navigate to the URL http://localhost:8080/HelloninjasStruts02/system.action. This will result in the screen shown below.

Output

Output of the complete code

Frequently Asked Questions

What exactly is Struts 2?

Struts 2 is an open-source web application framework that may be used to create Java EE web applications. It makes use of and extends the Java Servlet API in order to encourage developers to employ a model-view-controller (MVC) architecture.

What are the uses of the Struts 2 framework?

Struts 2 is a framework for creating MVC (Model View Controller)-based web applications. Struts 2 is a hybrid of the opensymphony webwork framework and struts 1.

webwork + struts1 = struts2

Write some Common Data tags? 

The Action Tag, The Include Tag, The Bean Tag, The Date Tag, The Param Tag, The Property Tag, The Text Tag, and The URL Tag are some of the common data tags.

What is the Property Tag?

The property tag is a generic tag that is used to retrieve a value's property, which defaults to the top of the stack if none is supplied.

How can you produce a WAR file?

Right-click on the project name and select Export > WAR File. After that, place this WAR in Tomcat's web apps directory. Finally, launch Tomcat and navigate to the URL http://localhost:8080/HelloninjasStruts02/system.action.

Conclusion 

In this article, we learned about the property tag, which includes Creating and Viewing the property tag.

After reading about the property tag, are you not feeling excited to read/explore more articles on the topic of Ruby? Don't worry; Coding Ninjas has you covered. To learn, see the Custom tag in JSF, The Merge Tag, and BorderLayout in Java.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! 

Do upvote our blogs if you find them helpful and engaging!

Live masterclass