Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
The Action Tag
3.
Action Tag Example
3.1.
Create Action Class
3.2.
Create View Pages
3.3.
Create configuration file
3.4.
Output
4.
Frequently Asked Questions
4.1.
What is structs in JAVA?
4.2.
What is the action tag do in structs 2?
4.3.
What is executeResult attribute in the action tag?
4.4.
What is the syntax of the action tag?
4.5.
What is the default value set to executeResult?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

The Action Tag

Author Teesha Goyal
0 upvote

Introduction 

Welcome to another Structs 2 control tags article where we will focus on the action Tag. We will illustrate the concept of the action tag by taking an example.

Structs is an open-source Java framework used for developing JAVA EE web applications. It allows the developers to build maintainable, flexible, and extensible web applications faster. 

Introduction to the Action Tag

The Action Tag

The action tag is a generic tag used to call the action directly from the view page (or JSP page) by specifying the action name. The attribute executeResult must be set to true to render the results directly to the current page. By default, the executeResult attribute is set to false; in this case, any result processor defined under this action name in structs.xml will be ignored. 

Syntax of Action Tag:

<s:action name="action_name" executeResult="true"/>

Action Tag Example

To understand the concept of action tag, we will discuss a complete example. In this example, we will first define an action class, then create the view pages, and at last configure files. 

We will walk you through each step. It will help you understand the concept of the Action tag better. 

Let’s begin! 😃

Create Action Class

We will create an action class containing a few methods to illustrate the use of the action tag. 

ActionNinja.java

package com.codingninjas.struts2;
import com.opensymphony.xwork2.ActionSupport;

public class ActionNinja extends ActionSupport{

	public String execute() {
		return SUCCESS;
	}

	public String greeting(){
		return "Hello there.";
	}

	public String wishMorning(){
		return "Good Morning Sunshine";
	}

	public String chantNinja(){
		System.out.println("Coding Ninjas! Coding Ninjas!");
		return "chantNinja";
	}
}

Create View Pages

Now comes the amazing part. We will be defining the view pages(JSP pages). Note that it is essential to specify executeResult as true to render the results directly to the current page. If you set executeResult as false or didn’t mention executeResult, the method will be executed, but nothing will be displayed. (executeResult is set to false by default)

To illustrate this, we will set the executeResult attribute to true for the greeting() and wishMorning() actions and use the default value for the chantNinja() action.

JSP

action.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
	Action Tag Example
</head>

<body>
	<h1>Example - Action Tag</h1>
	<ol>
		<li>
			Executing the action's result and rendering the page here. 
			<s:action name="greetingAction" executeResult="true"/>
		</li>

		<li>
			This time we will call action's wishMorning(). 
			<s:action name="greetingAction!wishMorning" executeResult="true"/>
		</li>

		<li>
			This time we only call the action's chantNinja() method; no result will be rendered, as executeResult is set false by default.
			<s:action name="greetingAction!chantNinja" />
		</li>
	</ol>
	
</body>
</html>

 

greeting.jsp

<html>
<head></head>
<body>
	<h2>Hello there, mate! Printed from greeting.jsp</h2>
</body>
</html>

 

wishMorning.jsp

<html>
<head></head>
<body>
	<h2> Good Morning, Ninja! Printed from wishMorning.jsp</h2>
</body>
</html>

 

chantNinja.jsp



<html>
<head></head>
<body>
	<h2>Coding Ninjas - Coding Ninjas Studio is the best! Printed from chantNinja.jsp</h2>
</body>
</html>

Create configuration file

Now we will have to configure the files and set up everything together. The extension of configuration files is .xml. We have created the below-shown structs.xml file:

structs.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="true" />
	<package name="default" namespace="/" extends="struts-default">

		<action name="actionNinja" 
			class="com.codingninjas.struts2.ActionNinja" >
			<result name="success">pages/action.jsp</result>
		</action>
			
		<action name="greetingAction" 
			class="com.codingninjas.struts2.ActionNinja" method="greeting">
			<result name="greeting">greeting.jsp</result>
			<result name="wishMorning">wishMorning.jsp</result>
			<result name="chantNinja">chantNinja.jsp</result>
		</action>
		
	</package>
</struts>

Output

http://localhost:8080/ActionTagExample/actionNinja.action

Output- Action Tag

You can see that in the first two statements, the action is called directly from the view page, and the results are rendered on the current page as the executeResult attribute is set to true. 

Although the action is executed in the last statement, the results are not rendered onto the current page as we have not specified the executeResult attribute as true, and it is set to false by default. 

Frequently Asked Questions

What is structs in JAVA?

Structs is an open-source Java framework used for developing JAVA EE web applications. Many developers use it as it allows them to build maintainable, flexible, and extensible web applications faster. 

What is the action tag do in structs 2?

The action tag is a generic tag used to call the action directly from the view page (or JSP page) by specifying the action name. 

What is executeResult attribute in the action tag?

The attribute executeResult in the action tag must be set to true to render the results directly to the current page. By default, the executeResult attribute is set to false; in this case, any result processor defined under this action name in structs.xml will be ignored. 

What is the syntax of the action tag?

The syntax of the action tag is:

<s:action name="action_name" executeResult="true"/>

Here, executeResult must be set true. 

What is the default value set to executeResult?

The default value of executeResult attribute of the action tag is false. If you want to render the results of action directly from the view page to the current page, it must be set to true.

Conclusion

This article discussed the Action tag in structs in JAVA. We also illustrated the concept of the Action tag using an example.

I hope you would have gained a better understanding of these topics now!

Are you planning to ace the interviews of reputed product-based companies like AmazonGoogleMicrosoft, and more? 

Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Thank You

Happy Coding!

Live masterclass