Struts 2 Action Tag Example

Struts 2 action tag is helpful for executing an action and inserting the result in a particular location of the web page. There are two options with action tag, if the value for the executeResult is set to “true”, then the result is displayed to the page as it is defined in that target view. However, if the executeResult attribute value is set to “false”, the result will not be displayed. Only the action will be executed and the context variables will be available for the use.

Lets see the below example to understand how to use s:action tax in Struts 2.

1. Create Action Classes

Struts2HelloWorldAction.java

package javabeat.net.struts2;

public class Struts2HelloWorldAction {
	private String userName;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String execute(){
		return "success";
	}
}

Struts2SimpleAction.java

package javabeat.net.struts2;

public class Struts2SimpleAction {
	public String execute(){
		return "success";
	}
}

2. Action Tag Example

ActionTag.jsp

<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:action name="simpleaction" executeResult="true">
      Output from Simple Action:  <br />
   </s:action>
</body>
</html>

Output.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	Simple JSP Page
</body>
</html>

3. Create struts.xml

Create struts.xml with required configurations to run the above example.

<?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="tags" extends="struts-default">
		<action name="actiontag" class="javabeat.net.struts2.Struts2HelloWorldAction"
			method="execute">
			<result name="success">/ActionTag.jsp</result>
		</action>
		<action name="simpleaction" class="javabeat.net.struts2.Struts2SimpleAction"
			method="execute">
			<result name="success">/Output.jsp</result>
		</action>
	</package>
</struts>

4. Run The Application

If you access the URL http://localhost:8080/Struts2App/actiontag, you would see the output.

Struts 2 Action Tag Example Screen

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*