Struts 2 “property” tag is used to get the property value from a class, which will default to the current Action class (top of the stack) property if none is specified. In this tutorial, it shows the use of “property” tag to get the property value from the current Action class and other bean class.

1. Action

An Action class, with a “name” property.

PropertyTagAction.java

package com.mkyong.common.action;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class PropertyTagAction extends ActionSupport{
 
	private String name = "Name from PropertyTagAction.java"; 
 
	public String getName() {
		return name;
	}
 
	public String execute() throws Exception {
 
		return SUCCESS;
	}
}

2. Bean

A simple Java class, with a “name” property.

Person.java

package com.mkyong.common;
 
public class Person {
 
	private String name = "Name from Person.java"; 
 
	public String getName() {
		return name;
	}
 
}

3. property tag example

It shows the use of “property” tag to get the “name” property value from the “PropertyTagAction” and “Person” class.

property.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
 
<body>
<h1>Struts 2 property tag example</h1>
 
<h4>1. Call getName() from propertyTagAction.java</h4> 
<s:property value="name" />
 
<h4>2. Call getName() from Person.java</h4> 
<s:bean name="com.mkyong.common.Person" var="personBean" />
<s:property value="#personBean.name" />
 
</body>
</html>
The “property.jsp” page is a success result page returned by the “PropertyTagAction” action. If you specified a <s:property value=”name” /> in “property.jsp” page, it will default to the current Action class “PropertyTagAction.getName()” property.

4. struts.xml

Link it ~

<?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="propertyTagAction" 
			class="com.mkyong.common.action.PropertyTagAction" >
			<result name="success">pages/property.jsp</result>
		</action>
 
	</package>
</struts>

5. Demo

http://localhost:8080/Struts2Example/propertyTagAction.action

Output

Struts 2 property tag example

Reference

  1. Struts 2 property tag documentation
Note : You can find more similar articles at - Struts 2.x Tutorials