Struts 2 action tag example
Struts 2 “action” tag is used to call action class directly from a JSP page. if the “executeResult” attribute is set to true, the content of the result page will be rendered directly in the current page.
This is best illustrated using a complete example :
1. Action
An Action class with few methods to forward the result to different result page.
ParamTagAction.java
package com.mkyong.common.action;
import com.opensymphony.xwork2.ActionSupport;
public class ActionTagAction extends ActionSupport{
public String execute() {
return SUCCESS;
}
public String sayHello(){
return "sayHello";
}
public String sayStruts2(){
return "sayStruts2";
}
public String saySysOut(){
System.out.println("SysOut SysOut SysOut");
return "saySysOut";
}
}
2. action tag example
JSP pages to show the use of “action” tag. If the executeResult=”true” is specified in the action tag, the method is executed and the result page will be displayed directly; Otherwise, it just execute the method, no result page will be displayed.
action.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
<body>
<h1>Struts 2 action tag example</h1>
<ol>
<li>
Execute the action's result, render the page here.
<s:action name="sayHelloAction" executeResult="true"/>
</li>
<li>
Doing the same as above, but call action's sayStruts2() method.
<s:action name="sayHelloAction!sayStruts2" executeResult="true"/>
</li>
<li>
Call the action's saySysOut() method only, no result will be rendered,
By defautlt, executeResult="false".
<s:action name="sayHelloAction!saySysOut" />
</li>
</ol>
</body>
</html>
sayHello.jsp
<html>
<head>
</head>
<body>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-2836379775501347"
data-ad-slot="8821506761"
data-ad-format="auto"
data-ad-region="mkyongregion"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><h2>Hello Hello Hello ~ from sayHello.jsp</h2>
</body>
</html>
sayStruts2.jsp
<html>
<head>
</head>
<body>
<h2>Struts 2 Struts 2 Struts 2 ~ from sayStruts2.jsp</h2>
</body>
</html>
saySysOut.jsp
<html>
<head>
</head>
<body>
<h2>SysOut SysOut SysOut ~ from saySysOut.jsp</h2>
</body>
</html>
3. struts.xml
Declared few result name to demonstrate the executeResult effect.
<?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="actionTagAction"
class="com.mkyong.common.action.ActionTagAction" >
<result name="success">pages/action.jsp</result>
</action>
<action name="sayHelloAction"
class="com.mkyong.common.action.ActionTagAction"
method="sayHello">
<result name="sayHello">sayHello.jsp</result>
<result name="sayStruts2">sayStruts2.jsp</result>
<result name="saySysOut">saySysOut.jsp</result>
</action>
</package>
</struts>
4. Demo
http://localhost:8080/Struts2Example/actionTagAction.action
Output

Leave a Reply
mkyong, please can you tell me about boolean attribute ignoreContextParams of
tag. how does it work?i want to open my jsp page on calling struts action (this action should not come in url ) .
like http://www.xyz.com it should hit action and display resulted jsp but url shold not be http://www.xyz.com/action.action
According to naming convertor post you mentioned at http://www.mkyong.com/struts2/struts-2-hello-world-annotation-example/
if above acion class was annotated the URL would have been
http://localhost:8080/Struts2Example/actionTag.action
Hi,
In my project i use struts2 jquery grid and json.
My action class return a json string .using that json string values i want build grid dynamically.
My problem is that
How to catch that json string and passed that string to iterator tag.
Please help me..
Thank u.
hello , in this example the part of enabling the Dynamic method invocation is missing in struts.xml … due to which it won’t be able to load the second call in the JSP ..