Struts 2 property tag example

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>

<h2>1. Call getName() from propertyTagAction.java</h2> 
<s:property value="name" />

<h2>2. Call getName() from Person.java</h2> 
<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

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
2 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
LevanVincentStruts 2 Tutorial Recent comment authors
newest oldest most voted
Vincent
Guest
Vincent

I have seen some symbols from books. but they have not explain the meaning of them.
1) value=”xx”
2) value=”#xx”
3) value=”%{xx}”
4) value=”${xx}”
Would you tell me what is the meaning of them?

trackback
Struts 2 Tutorial

[…] with the current action.param tag example Struts 2 param tag is used to parametrize other tags.property tag example Struts 2 property tag is used to get the property value from a class, which will default to the […]