Struts - SPLessons

Chapter 11

Struts 2 Property Tags

Struts 2 Property Tags

Struts 2 Property Tags, While developing on the projects, developer needs to to write the code, then some tags will help to reduce the code where each and every tag will have its own functionality but all the tags functionality is same but their usage may be different in technologies. Struts 2 have some set of tags. Using these tags, user can easily execute the flow of page execution. Struts 2 has the great feature that can integrate with other frameworks such as hibernate, tiles, spring, tiles, Ajax. Following are the different tags available in Struts 2 will help to reduce the programming length.


Property Tag

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.

<s:property value=”name”/>

Following is an example which describes more about the property tag and how it works functionally.
Person.java

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

}

Here just declared the text by using String and Get method has been performed to get the data.
PropertyTagAction.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;
	}
}

The execute() will execute the String and returns the success.
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.splessons.Person" var="personBean" />
<s:property value="#personBean.name" />

</body>
</html>

The getName() is used to get the name from propertyTagAction.java and Person.java.
struts.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>

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

<!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>

Output
When compile the program output will be as follows.

Summary

  • Struts 2 Property Tags is by default defined in stack.
  • Struts 2 Property Tags may have separate file needs to be kept along with HTML, JSP files.