« Previous | 1 | 2 | 3 | Next »
The Struts 2 dat tags are primarily used to manipulate the data displayed on a page. Listed below are the important data tags: <Start here>

The Action Tag
This tag enables developers to call actions directly from a JSP page by specifying the action name and an optional namespace. The body content of the tag is used to render the results from the Action. Any result processor defined for this action in struts.xml will be ignored, unless the executeResult parameter is specified.

<div>Tag to execute the action</div>
<br />
<s:action name="actionTagAction" executeResult="true" />
<br />
<div>To invokes special method in action class</div>
<br />
<s:action name="actionTagAction!specialMethod" executeResult="true" />

The Action Tag – Detailed Example
The action tag allows the programmers to execute an action from the view page. They can achieve this by specifying the action name. They can set the "executeResult" parameter to "true" to render the result directly in the view. Or, they can set this parameter to "false", but make use of the request attributes exposed by the action method.

Create Action Class
package com.jtc.struts2;
public class HelloWorldAction{
private String name;
public String execute() throws Exception {
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Create Views
Let us have HelloWorld.jsp to demonstrate the use of the generator tag:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>Example of Generator Tag</h2>
<h3>The colours of rainbow:</h3>
<s:generator val="%{'Violet,Indigo,Blue,
Green,Yellow,Orange,Red '}" count="7"
separator=",">
<s:iterator>
<s:property /><br/>
</s:iterator>
</s:generator>
</body>
</html>

Next let us have employees.jsp with the following content:

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Employees</title>
</head>
<body>
<s:action name="hello" executeResult="true">
Output from Hello: <br />
</s:action>
</body>
</html>

Configuration Files
Your struts.xml should look like:
<?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="helloworld" extends="struts-default">
<action name="hello"
class="com.jtc.struts2.HelloWorldAction"
method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
<action name="employee"
class="com.jtc.struts2.Employee"
method="execute">
<result name="success">/employee.jsp</result>
</action>
</package>
</struts>

Your web.xml should look like:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

Right click on the project name and click Export > WAR File to create a War file. Then deploy this WAR in the Tomcat's webapps directory.
Finally, start Tomcat server and try to access URL http://localhost:8080/HelloWorldStruts2/employee.action. This will produce the following screen:

As you can see in this example, we have specified the value of executeResult to "true". Therefore the outcome of the hello.action is rendered directly in the page. The HelloWorld.jsp prints the colors of the rainbow - which is now rendered within employee.jsp
Now, let us modify the HelloWorldAction.java slightly:

package com.jtc.struts2;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.ServletActionContext;
public class HelloWorldAction{
private String name;
public String execute()
{
List names = new ArrayList();
names.add("Robert");
names.add("Page");
names.add("Kate");
ServletActionContext.getRequest().setAttribute("names", names);
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Finally, modify the employee.jsp as follows:

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Employees</title>
</head>
<body>
<s:action name="hello" executeResult="false">
Output from Hello: <br />
</s:action>
<s:iterator value="#attr.names">
<s:property /><br />
</s:iterator>
</body>
</html>

Again, right click on the project name and click Export > WAR File to create a War file. Then deploy this WAR in the Tomcat's webapps directory.
Finally, start Tomcat server and try to access URL http://localhost:8080/HelloWorldStruts2/employee.action. This will produce the following screen:

The Include Tag
These include will be used to include a JSP file in another JSP page.
<-- First Syntax -->
<s:include value="myJsp.jsp" />
<-- Second Syntax -->
<s:include value="myJsp.jsp">
<s:param name="param1" value="value2" />
<s:param name="param2" value="value2" />
</s:include>
<-- Third Syntax -->
<s:include value="myJsp.jsp">
<s:param name="param1">value1</s:param>
<s:param name="param2">value2</s:param>
</s:include>

The Include Tag – Detailed Example
The Struts include tag is very similar to the jsp include tag and it is rarely used. We have seen how to include the output of a struts action into a jsp using the <s:action> tags. The <s:include> tag is slightly different. It allows you to include the output of a jsp, servlet or any other resource (something other than a struts action) into a jsp. Behind the scenes it is exactly similar to the , but it allows you to pass parameters to the included file and it is also part of Struts framework.
The following example shows how we will include the output of HelloWorld.jsp into the employee.jsp. In this case, the action method in HelloWorldAction.java will not be invoked, as we are directly including the jsp.

Create Action Class
package com.jtc.struts2;
public class HelloWorldAction{
private String name;
public String execute() throws Exception {
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Create Views
Let us have HelloWorld.jsp with the following content:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>Example of Generator Tag</h2>
<h3>The colours of rainbow:</h3>
<s:generator val="%{'Violet,Indigo,Blue,
Green,Yellow,Orange,Red '}" count="7"
separator=",">
<s:iterator>
<s:property /><br/>
</s:iterator>
</s:generator>
</body>
</html>

Next let us have employees.jsp with the following content:

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Employees</title>
</head>
<body>
<p>An example of the include tag: </p>
<s:include value="HelloWorld.jsp"/>
</body>
</html>

Configuration Files
Your struts.xml should look like:
<?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="helloworld" extends="struts-default">
<action name="hello"
class="com.jtc.struts2.HelloWorldAction"
method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
<action name="employee"
class="com.jtc.struts2.Employee"
method="execute">
<result name="success">/employee.jsp</result>
</action>
</package>
</struts>

Your web.xml should look like:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

Right click on the project name and click Export > WAR File to create a War file. Then deploy this WAR in the Tomcat's webapps directory.
Finally, start Tomcat server and try to access URL http://localhost:8080/HelloWorldStruts2/employee.action. This will produce the following screen:

The Bean Tag
These bean tag instantiates a class that conforms to the JavaBeans specification. This tag has a body which can contain a number of Param elements to set any mutator methods on that class. If the var attribute is set on the BeanTag, it will place the instantiated bean into the stack's Context.
<s:bean name="org.apache.struts2.util.Counter" var="counter">
<s:param name="first" value="20"/>
<s:param name="last" value="25" />
</s:bean>

The Bean Tag – Detailed Example
The bean tag is a combination of the set and push tags, it allows you create a new instance of an object and then set the values of the variables. It then makes the bean available in the valuestack, so that it can be used in the JSP page.
The Bean tag requires a java bean to work with. So, the standard java bean laws should be followed. Which is, the bean should have a no argument constructor. All properties that you want to expose and use should have the getter and setter

methods. For the purpose of this exercise, let us use the following Counter class that comes in the struts util package. The Counter class is a bean that can be used to keep track of a counter.
Let us keep all the files unchanged and modify HelloWorld.jsp file.

Create Action Class
package com.jtc.struts2;
public class HelloWorldAction{
private String name;
public String execute() throws Exception {
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Create Views
Let us have HelloWorld.jsp with the following content:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<s:bean name="org.apache.struts2.util.Counter" var="counter">
<s:param name="first" value="20"/>
<s:param name="last" value="25" />
</s:bean>
<ul>
<s:iterator value="#counter">
<li><s:property /></li>
</s:iterator>
</ul>
</body>
</html>

Next let us have employees.jsp with the following content:

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Employees</title>
</head>
<body>
<p>An example of the include tag: </p>
<s:include value="HelloWorld.jsp"/>
</body>
</html>

Configuration Files
Your struts.xml should look like:
<?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="helloworld" extends="struts-default">
<action name="hello"
class="com.jtc.struts2.HelloWorldAction"
method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
<action name="employee"
class="com.jtc.struts2.Employee"
method="execute">
<result name="success">/employee.jsp</result>
</action>
</package>
</struts>

Your web.xml should look like:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

Right click on the project name and click Export > WAR File to create a War file. Then deploy this WAR in the Tomcat's webapps directory.
Finally, start Tomcat server and try to access URL http://localhost:8080/HelloWorldStruts2/hello.action. This will produce the following screen:

In this example, we are instantiating a new instance of the org.apache.struts2.util.Counter bean. We then set the first property to 20 and the last property to 25.
This means that the counter will have the values 20,21,22,23,24 and 25. We give the bean a name "counter". The struts bean tag instantiates the bean and puts it in the value stack. We can now use the iterator to go through the Counter bean and print out the value of the counter.

The Date Tag
These date tag will allow you to format a Date in a quick and easy way. You can specify a custom format (eg. "dd/MM/yyyy hh:mm"), you can generate easy readable notations (like "in 2 hours, 14 minutes"), or you can just fall back on a predefined format with key 'struts.date.format' in your properties file.
<s:date name="person.birthday" format="dd/MM/yyyy" />
<s:date name="person.birthday" format="%{getText('some.i18n.key')}" />
<s:date name="person.birthday" nice="true" />
<s:date name="person.birthday" />

The Date Tag - Detailed Example
The date tag allows to format a Date in a quick and easy way. User can specify a custom format (eg. "dd/MM/yyyy hh:mm"), can generate easy readable notations (like "in 2 hours, 14 minutes"), or can just fall back on a predefined format with key 'struts.date.format' in the properties file.

Create Action Class
package com.jtc.struts2;
import java.util.*;
public class HelloWorldAction{
private Date currentDate;
public String execute() throws Exception{
setCurrentDate(new Date());
return "success";
}
public void setCurrentDate(Date date){
this.currentDate = date;
}
public Date getCurrentDate(){
return currentDate;
}
}

Create Views
Let us have HelloWorld.jsp with the following content:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>Current Date</h2>
<h3>Day/Month/Year Format</h3>
<s:date name="currentDate" format="dd/MM/yyyy" />
<h3>Month/Day/Year Format</h3>
<s:date name="currentDate" format="MM/dd/yyyy" />
</body>
</html>

Configuration Files
Your struts.xml should look like:
<?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="helloworld" extends="struts-default">
<action name="hello"
class="com.jtc.struts2.HelloWorldAction"
method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>

Your web.xml should look like:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

Right click on the project name and click Export > WAR File to create a War file. Then deploy this WAR in the Tomcat's webapps directory.
Finally, start Tomcat server and try to access URL http://localhost:8080/HelloWorldStruts2/hello.action. This will produce the following screen:

The Param Tag
These param tag can be used to parameterize other tags. This tag has the following two parameters.
  • name (String) - the name of the parameter
  • value (Object) - the value of the parameter

<pre>
<ui:component>
<ui:param name="key" value="[0]"/>
<ui:param name="value" value="[1]"/>
<ui:param name="context" value="[2]"/>
</ui:component>
</pre>

The Param Tag – Detailed Example
The param tag can be used to parameterize other tags. The include tag and bean tag are examples of such tags. Let us take same example which we have discussed while discussing bean tag.

Create Action Class
package com.jtc.struts2;
public class HelloWorldAction{
private String name;
public String execute() throws Exception {
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Create Views
Let us have HelloWorld.jsp with the following content:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<s:bean name="org.apache.struts2.util.Counter" var="counter">
<s:param name="first" value="20"/>
<s:param name="last" value="25" />
</s:bean>
<ul>
<s:iterator value="#counter">
<li><s:property /></li>
</s:iterator>
</ul>
</body>
</html>

Next let us have employees.jsp with the following content:

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Employees</title>
</head>
<body>
<p>An example of the include tag: </p>
<s:include value="HelloWorld.jsp"/>
</body>
</html>

Configuration Files
Your struts.xml should look like:
<?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="helloworld" extends="struts-default">
<action name="hello"
class="com.jtc.struts2.HelloWorldAction"
method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
<action name="employee"
class="com.jtc.struts2.Employee"
method="execute">
<result name="success">/employee.jsp</result>
</action>
</package>
</struts>

Your web.xml should look like:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
</web-app>

Right click on the project name and click Export > WAR File to create a War file. Then deploy this WAR in the Tomcat's webapps directory.
Finally, start Tomcat server and try to access URL http://localhost:8080/HelloWorldStruts2/hello.action. This will produce the following screen:

In this example, we are instantiating a new instance of the org.apache.struts2.util.Counter bean. We then set the first property to 20 and the last property to 25. This means that the counter will have the values 20,21,22,23,24 and 25. We give the bean a name "counter". The struts bean tag instantiates the bean and puts it in the value stack. We can now use the iterator to go through the Counter bean and print out the value of the counter.

The Property Tag
These property tag is used to get the property of a value, which will default to the top of the stack if none is specified.
<s:push value="myBean">
<!-- Example 1: -->
<s:property value="myBeanProperty" />
<!-- Example 2: -->TextUtils
<s:property value="myBeanProperty" default="a default value" />
</s:push>

The Property Tag – Detailed Example
The property tag is used to get the property of a value, which will default to the top of the stack if none is specified. This example shows you the usage of three simple data tags - namely set, push and property.

Create Action Classes
For this exercise, let us reuse examples given in "Data Type Conversion" chapter but with little modifications. So let us start with creating classes. Consider the following POJO class Environment.java.
package com.jtc.struts2;
public class Environment {
private String name;
public Environment(String name)
{
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Let us have following action class:

package com.jtc.struts2;
import com.opensymphony.xwork2.ActionSupport;
public class SystemDetails extends ActionSupport {
private Environment environment = new Environment("Development");
private String operatingSystem = "Windows XP SP3";
public String execute()
{
return SUCCESS;
}
public Environment getEnvironment() {
return environment;
}
public void setEnvironment(Environment environment) {
this.environment = environment;
}
public String getOperatingSystem() {
return operatingSystem;
}
public void setOperatingSystem(String operatingSystem) {
this.operatingSystem = operatingSystem;
}
}

Create Views
Let us have System.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"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>System Details</title>
</head>
<body>
<p>The environment name property can be accessed in three ways:</p>
(Method 1) Environment Name:
<s:property value="environment.name"/><br/>
(Method 2) Environment Name:
<s:push value="environment">
<s:property value="name"/><br/>
</s:push>
(Method 3) Environment Name:
<s:set name="myenv" value="environment.name"/>
<s:property value="myenv"/>
</body>
</html>

Let us now go through the three options one by one:
  • In the first method, we use the property tag to get the value of the environment's name. Since, the environment variable is in the action class, it is automatically available in the value stack. We can directly refer to it using the property environment.name. Method 1 works fine, when you have limited number of properties in a class. Imagine if you have 20 properties in the Environment class. Every time you need to refer to these variables you need to add "environment." as the prefix. This is where the push tag comes in handy.
  • In the second method, we push the "environment" property to the stack. Therefore, now within the body of the push tag, the environment property is available at the root of the stack. From now, you can refer to the property quite easily as shown in the example.
  • In the final method, we use the set tag to create a new variable called myenv. This variable's value is set to environment.name. So, now we can use this variable wherever we refer to the environment's name.
Configuration Files
Your struts.xml should look like:
<?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="helloworld" extends="struts-default">
<action name="system"
class="com.jtc.struts2.SystemDetails"
method="execute">
<result name="success">/System.jsp</result>
</action>
</package>
</struts>

Your web.xml should look like:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

Right click on the project name and click Export > WAR File to create a War file. Then deploy this WAR in the Tomcat's webapps directory.
Finally, start Tomcat server and try to access URL http://localhost:8080/HelloWorldStruts2/system.action. This will produce the following screen:

The Push Tag
These push tag is used to push value on stack for simplified usage.
<s:push value="user">
<s:propery value="firstName" />
<s:propery value="lastName" />
</s:push>

The Push Tag – Detailed Example
The property tag is used to get the property of a value, which will default to the top of the stack if none is specified. This example shows you the usage of three simple data tags - namely set, push and property.

Create Action Classes
For this exercise, let us reuse examples given in "Data Type Conversion" chapter but with little modifications. So let us start with creating classes. Consider the following POJO classEnvironment.java.
package com.jtc.struts2;
public class Environment {
private String name;
public Environment(String name)
{
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Let us have the following action class:

package com.jtc.struts2;
import com.opensymphony.xwork2.ActionSupport;
public class SystemDetails extends ActionSupport {
private Environment environment = new Environment("Development");
private String operatingSystem = "Windows XP SP3";
public String execute()
{
return SUCCESS;
}
public Environment getEnvironment() {
return environment;
}
public void setEnvironment(Environment environment) {
this.environment = environment;
}
public String getOperatingSystem() {
return operatingSystem;
}
public void setOperatingSystem(String operatingSystem) {
this.operatingSystem = operatingSystem;
}
}

Create Views
Let us have System.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"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>System Details</title>
</head>
<body>
<p>The environment name property can be accessed in three ways:</p>
(Method 1) Environment Name:
<s:property value="environment.name"/><br/>
(Method 2) Environment Name:
<s:push value="environment">
<s:property value="name"/><br/>
</s:push>
(Method 3) Environment Name:
<s:set name="myenv" value="environment.name"/>
<s:property value="myenv"/>
</body>
</html>

Let us now go through the three options one by one:
  • In the first method, we use the property tag to get the value of the environment's name. Since the environment variable is in the action class, it is automatically available in the value stack. We can directly refer to it using the propertyenvironment.name. Method 1 works fine, when you have limited number of properties in a class. Imagine if you have 20 properties in the Environment class. Every time you need to refer to these variables you need to add "environment." as the prefix. This is where the push tag comes in handy.
  • In the second method, we push the "environment" property to the stack. Therefore now within the body of the push tag, the environment property is available at the root of the stack. So you now refer to the property quite easily as shown in the example.
  • In the final method, we use the set tag to create a new variable called myenv. This variable's value is set to environment.name. So, now we can use this variable wherever we refer to the environment's name.

Configuration Files
Your struts.xml should look like:
<?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="helloworld" extends="struts-default">
<action name="system"
class="com.jtc.struts2.SystemDetails"
method="execute">
<result name="success">/System.jsp</result>
</action>
</package>
</struts>

Your web.xml should look like:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

Right click on the project name and click Export > WAR File to create a War file. Then deploy this WAR in the Tomcat's webapps directory.
Finally, start Tomcat server and try to access URL http://localhost:8080/HelloWorldStruts2/system.action. This will produce the following screen:

The Set Tag
These set tag assigns a value to a variable in a specified scope. It is useful when you wish to assign a variable to a complex expression and then simply reference that variable each time rather than the complex expression. The scopes available are application, session, request, page and action.
<s:set name="myenv" value="environment.name"/>
<s:property value="myenv"/>

The Set Tag – Detailed Example
The property tag is used to get the property of a value, which will default to the top of the stack if none is specified. This example shows you the usage of three simple data tags - namely set, push and property.

Create Action Classes
For this exercise, let us reuse examples given in "Data Type Conversion" chapter but with little modifications. So let us start with creating classes. Consider the following POJO class Environment.java.
package com.jtc.struts2;
public class Environment {
private String name;
public Environment(String name)
{
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Let us have the following action class:

package com.jtc.struts2;
import com.opensymphony.xwork2.ActionSupport;
public class SystemDetails extends ActionSupport {
private Environment environment = new Environment("Development");
private String operatingSystem = "Windows XP SP3";
public String execute()
{
return SUCCESS;
}
public Environment getEnvironment() {
return environment;
}
public void setEnvironment(Environment environment) {
this.environment = environment;
}
public String getOperatingSystem() {
return operatingSystem;
}
public void setOperatingSystem(String operatingSystem) {
this.operatingSystem = operatingSystem;
}
}

Create Views
Let us have System.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"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>System Details</title>
</head>
<body>
<p>The environment name property can be accessed in three ways:</p>
(Method 1) Environment Name:
<s:property value="environment.name"/><br/>
(Method 2) Environment Name:
<s:push value="environment">
<s:property value="name"/><br/>
</s:push>
(Method 3) Environment Name:
<s:set name="myenv" value="environment.name"/>
<s:property value="myenv"/>
</body>
</html>

Let us now go through the three options one by one:
  • In the first method, we use the property tag to get the value of the environment's name. Since the environment variable is in the action class, it is automatically available in the value stack. We can directly refer to it using the propertyenvironment.name. Method 1 works fine, when you have limited number of properties in a class. Imagine if you have 20 properties in the Environment class. Every time you need to refer to these variables you need to add "environment." as the prefix. This is where the push tag comes in handy.
  • In the second method, we push the "environment" property to the stack. Therefore now within the body of the push tag, the environment property is available at the root of the stack. So you now refer to the property quite easily as shown in the example.
  • In the final method, we use the set tag to create a new variable called myenv. This variable's value is set to environment.name. So, now we can use this variable wherever we refer to the environment's name.

Configuration Files
Your struts.xml should look like:
<?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="helloworld" extends="struts-default">
<action name="system"
class="com.jtc.struts2.SystemDetails"
method="execute">
<result name="success">/System.jsp</result>
</action>
</package>
</struts>

Your web.xml should look like:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

Right click on the project name and click Export > WAR File to create a War file. Then deploy this WAR in the Tomcat's webapps directory.
Finally, start Tomcat server and try to access URL http://localhost:8080/HelloWorldStruts2/system.action. This will produce the following screen:

The Text Tag
These text tag is used to render a I18n text message.
<!-- First Example -->
<s:i18n name="struts.action.test.i18n.Shop">
<s:text name="main.title"/>
</s:i18n>
<!-- Second Example -->
<s:text name="main.title" />
<!-- Third Examlpe -->
<s:text name="i18n.label.greetings">
<s:param >Mr Smith</s:param>
</s:text>

The Text Tag – Detailed Example
The text tag is a generic tag that is used to render a I18n text message. Follow one of the three steps:
  • The message must be in a resource bundle with the same name as the action that it is associated with. In practice this means that you should create a properties file in the same package as your Java class with the same name as your class, but with .properties extension.
  • If the named message is not found, then the body of the tag will be used as default message.
  • If no body is used, then the name of the message will be used.
  • Let us check the following example to understand the usage of text tag:

Create Action Classes
package com.jtc.struts2;
public class HelloWorldAction{
private String name;
public String execute() throws Exception {
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Create Views
Let us have HelloWorld.jsp with the following content:
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Text Tag Example</title>
</head>
<body>
<s:i18n name="HelloWorldAction">
<s:text name="name.success"/><br>
<s:text name="name.xyz">Message doesn't exists</s:text><br>
<s:text name="name.msg.param">
<s:param >ZARA</s:param>
</s:text>
</s:i18n>
</body>
</html>

Configuration Files
Let us create a property file with the same name as of your action class package name. So in this case we will create HelloWorldAction.properties file and keep in the class path:
name.success = This is success message
name.msg.param = The param example - param : {0}

Your struts.xml should look like:

<?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" />
<constant name="struts.custom.i18n.resources"
<alue="ApplicationResources"/>
<package name="helloaction" extends="struts-default">
<action name="hello"
class="com.jtc.struts2.HelloWorldAction"
method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>

Your web.xml should look like:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

Right click on the project name and click Export > WAR File to create a War file. Then deploy this WAR in the Tomcat's webapps directory. Finally, start Tomcat server and try to access URL http://localhost:8080/HelloWorldStruts2/hello.action. This will produce the following screen:


The URLTag

These url tag is used to create a URL.

<-- Example 1 -->
<s:url value="editGadget.action">
<s:param name="id" value="%{selected}" />
</s:url>
<-- Example 2 -->
<s:url action="editGadget">
<s:param name="id" value="%{selected}" />
</s:url>
<-- Example 3-->
<s:url includeParams="get">
<s:param name="id" value="%{'22'}" />
</s:url>

The url tag is responsible for generating URL strings. The advantage of this is that you can supply parameters to the tag. Let us go through an example to show the usage of url tag.

Create Action Classes
package com.jtc.struts2;
public class HelloWorldAction{
private String name;
public String execute() throws Exception {
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Create Views
Let us have HelloWorld.jsp with the following content:
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<s:url id="login" action="login" var="myurl">
<s:param name="user">Zara</s:param>
</s:url>
<a href='<s:property value="#myurl"/>'>
<s:property value="#myurl"/></a>
</body>
</html>

Here we are generating a url link to the "login.action". We have given this url a name "myurl". This is so that we can reuse this url link in multiple places within the jsp file. We then supply the url with a parameter called “USER”. The parameter value is actually appended to the query string as you can see from the output above.
The URL tag is mainly useful when you want to create a dynamic hyperlink based on a bean's property value.

Configuration Files
Your struts.xml should look like:
<?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="helloaction" extends="struts-default">
<action name="hello"
class="com.jtc.struts2.HelloWorldAction"
method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>

Your web.xml should look like:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

Right click on the project name and click Export > WAR File to create a War file. Then deploy this WAR in the Tomcat's webapps directory. Finally, start Tomcat server and try to access URL http://localhost:8080/HelloWorldStruts2/hello.action. This will produce the following screen:

« Previous | 1 | 2 | 3 | Next »


copyright © 2014 - all rights riserved by javatechnologycenter.com