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.
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.
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;
}
}
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.