Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Struts 2 Action
3.
Action Interface
4.
Method of Action Interface
5.
ActionSupport Class
6.
Multiple Action
7.
Frequently Asked Questions
7.1.
What is Struts2?
7.2.
Describe a few functional annotations that Struts2 included.
7.3.
What are the recommended practices for creating applications using Struts 2?
7.4.
What are Struts2 core components?
7.5.
What are the different ways of creating Action classes in Struts2?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Struts 2 Action

Author yuvatimankar
0 upvote

Introduction

Before knowing about struts2 Action, first, let's see what the Struts framework is. Struts is an open-source Java framework that Apache designs. Apache provides users a free source to download Struts. Struts follow MVC architecture and consist of models: Model0, Model1, and Model2. It includes the execution of MVC architecture. Struts provide pre-built classes for MVC that we can use or extend. Only developers can implement the Struct application. 

Struts

Actions are the base of the struts2 framework because they are for any Model View Controller framework. Each URL we are creating is portrayed to a specific action, which will provide the processing logic which is required to service the request from the user. But the, Action also contributes to two other capacities. Firstly, the Action plays a vital role in transferring data from the request via view, either a JSP or other types of result. Secondly, 

The Action must serve the framework in discovering which result should render the view that will be returned in the reaction to the request.

Struts 2 Action

Struts2 Action

In struts 2, the action class is Plain Old Java Object(POJO). POJO means we are not forced to execute any interface or extend any class. Basically, execute method should be specified that represents the business logic. The normal action class looks like this:

package com.codingninjas;  
public class Welcome {  
public String execute(){  
    return "success";  
}  
}  

Action Interface

A convenient method is the execute the com.opensymphony.xwork2.Action interface that defines five constants and one execute method.

Five constants of Action Interface are as follows:

SUCCESS

This constant indicates that action implementation is successful, and a successful result should be displayed to the user.

ERROR

The error indicates that action execution is failed, and an error result should be shown to the user.

LOGIN

This constant indicates that the user is not logged in, and a login result should be displayed to the user.

INPUT

This indicates that validation is failed, and an input result should be displayed to the user again.

NONE

This constant indicates that action execution is successful, but the user should display no result.

Now let's see what values are assigned to these constants:

public static final String ERROR = "error"; 
public static final String SUCCESS = "success";  
public static final String LOGIN  = "login";  
public static final String INPUT = "input";  
public static final String NONE = "none";  

Method of Action Interface

The action interface stores a single method executed that should be implemented overridden by the action class even if we are not forced.

Public String execute();

 

Example of Struts Action that implements Action interface

If we execute the Action interface, we can directly use the constants in place of values.

Welcome.java

package com.codingninjas;  
import com.opensymphony.xwork2.Action;  
public class Welcome implements Action{  
public String execute(){  
    return SUCCESS;  
}  
} 

ActionSupport Class

It is an appropriate class that implements multiple interfaces such as Validateable, Action, ValidationAware, TextProvider, Serializable, and LocaleProvider. Therefore, it is mostly used in place of Action.

Example of Struts Action that extends ActionSupport class

Welcome.java

package com.codingninjas;  
import com.opensymphony.xwork2.ActionSupport;  
public class Welcome extends ActionSupport{  
public String execute(){  
    return SUCCESS;  
}  
}

Multiple Action

Multiple Actions

We will define multiple actions to handle the different requests and to give different URL's to the user; according to that, we will define different classes, which are as follows:

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


class MyAction extends ActionSupport {
   public static String GOOD = SUCCESS;
   public static String BAD = ERROR;
}


public class HelloWorld extends ActionSupport {
   ...
   public String execute() {
      if ("SECRET".equals(name)) return MyAction.GOOD;
      return MyAction.BAD;
   }
   ...
}


public class SomeOtherClass extends ActionSupport {
   ...
   public String execute() {
      return MyAction.GOOD;
   }
   ...
}


We will configure these actions in struts.xml as follows:

<?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.tutorialspoint.struts2.HelloWorld" 
         method = "execute">
         <result name = "success">/HelloWorld.jsp</result>
         <result name = "error">/AccessDenied.jsp</result>
      </action>
      
      <action name = "something" 
         class = "com.codingninjas.struts2.SomeOtherClass" 
         method = "execute">
         <result name = "success">/Something.jsp</result>
         <result name = "error">/AccessDenied.jsp</result>
      </action>
   </package>
</struts>

 

Must Read Apache Server

Frequently Asked Questions

What is Struts2?

Apache Struts2 is an open-source framework for building web applications in java.

It is far better than struts 1, increasing flexibility and usability. We have many tags and some new additional features like type convertors which make it easily extensible.

Describe a few functional annotations that Struts2 included.

Some of the essential annotations that were introduced in struts2 are:

@Action, @Namespace, @Result, @ResultPath, @Actions, etc. These are some of a few annotations used in every application of Struts2.

What are the recommended practices for creating applications using Struts 2?

Attempt always to extend the struts-default package when establishing your package. To prevent code repeatedly while using interceptors, For code reuse and ModelDriven interface implementation, action classes' Java bean properties should always be kept in a distinct bean.

What are Struts2 core components?

Struts2 core components are

Action classes, Interceptors, Result pages, Tag libraries, and JSP is some of the few core components.

What are the different ways of creating Action classes in Struts2?

For Creating Action classes, we use four methods. These four methods are: by implementing an action interface, using Struts2 @Action annotations, any regular Java class containing an execute method, and a String return value may be set up as an action class, by adding to the ActionSupport class.

Conclusion

In this article, we learned about struts2. We started with the introduction of struts2 and then we saw struts2 action, action interface, method of action interface, and multiple actions in struts2. 

After reading about building a project in Struts 2, refer to Struts 2 documentationStruts Tutorial for beginnersStruts 2 Intro, and 15 Best Java frameworks to use in 2022-Coding Ninjas or a deeper understanding of Struts 2 development and other related topics.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Thank you
Live masterclass