The Struts 2 data tags are mostly used to display the manipulate data on a page, Struts 2 Property Tags is the data tag. These
property tag is used to get the property of a value, which is by default defined in stack.
[c]
<s:property value=”name”/>
[/c]
Following is an example which describes more about the property tag and how it works functionally.
Person.java
[java]public class Person {
private String name = "Name from Person.java";
public String getName() {
return name;
}
}[/java]
Here just declared the text by using String and Get method has been performed to get the data.
PropertyTagAction.java
[java]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;
}
}[/java]
The
execute() will execute the String and returns the success.
property.jsp
[html]<%@ 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.splessons.Person" var="personBean" />
<s:property value="#personBean.name" />
</body>
</html>[/html]
The
getName() is used to get the name from
propertyTagAction.java and
Person.java.
struts.xml
[xml]<?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="false" />
<package name="default" namespace="/" extends="struts-default">
<action name="propertyTagAction"
class="com.splessons.PropertyTagAction" >
<result name="success">pages/property.jsp</result>
</action>
</package>
</struts>[/xml]
By default, the
development mode is disabled, because it has a significant impact on performance, since the entire configuration will be reloaded on every request.
The development mode is only suitable in a development or debugging environment. In a production environment, you have to disable it. It will cause significant impact on performance, because the entire application configuration and properties files will be reloaded on every request and extra logging and debugging information will also be provided.
web.xml
[xml]<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Struts 2 Web Application</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>[/xml]
Output
When compile the program output will be as follows.