Struts 2 ActionSupport

Struts 2 ActionSupport

In this article, I am going to discuss Struts 2 ActionSupport with Examples. Please read our previous article where we discussed Steps to Create a Struts 2 Application.

What is ActionSupport in Struts 2?

In Struts 2, ActionSupport is a class provided by the framework that serves as a base class for action classes. It is an implementation of the Action interface and provides additional convenience methods and features to simplify action development. Here are some key points about ActionSupport:

  • It provides default implementations for common methods defined in the Action interface, such as execute() and validate(). By extending ActionSupport, you can focus on implementing the specific logic of your action without needing to implement all the methods of the Action interface.
  • ActionSupport includes methods to handle validation and error messages. It has built-in support for validation through integration with the XWork validation framework. You can define validation rules in the action class or in external XML files, and ActionSupport provides methods to easily add and retrieve field errors and error messages.
  • It offers methods to handle internationalization (i18n) and localization. ActionSupport provides convenient methods to retrieve localized messages from resource bundles, making it easier to support multilingual applications.
  • ActionSupport also provides methods to manage action-related properties, such as setting and retrieving the HttpServletRequest and HttpServletResponse objects, accessing the session, and interacting with the value stack.

By extending ActionSupport, you can leverage these features and simplify the development of Struts 2 actions by inheriting common functionality and taking advantage of the framework’s built-in support for validation, i18n, and error handling.

Implementing ActionSupport in Struts 2

In this project, we will be using the ActionSupport interface to implement the action class for struts2. Follow the steps to do so:

For this project, we will be using the same project from the previous article (Struts2 Basic Application). It should contain the following files:

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.dotnet</groupId>
  <artifactId>strutex</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <dependencies>
   <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>6.1.2.1</version>
   </dependency>
   
   <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-convention-plugin</artifactId>
    <version>6.1.2.1</version>
   </dependency>
   
   <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts-taglib</artifactId>
    <version>1.3.10</version>
   </dependency>
   
   <dependency>
    <groupId>jakarta.servlet</groupId>
    <artifactId>jakarta.servlet-api</artifactId>
    <version>6.0.0</version>
   </dependency>
  </dependencies>
  
  <build>
   <plugins>
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
    	  <artifactId>maven-compiler-plugin</artifactId>
    	  <version>3.11.0</version>
    	  <configuration>
     <source>17</source>
     <target>17</target>
    </configuration>
    </plugin>
   </plugins>
  </build>
</project>
src/main/webapp/WEB-INF/web.xml
<?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"
xsi:schemalocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/web-app_3_0.xsd"
id="apache-struts-config-example"
version="3.0">

 <display-name>Struts Config XML</display-name>
 
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
 </filter>
 
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

</web-app>
src/main/webapp/index.html
<!DOCTYPE html>
<html>
 <head>
  <title>Example of Struts</title>
 </head>
 
 <body>
  <form action="hello.action">
   <p>What is your name?</p>
   <input type="text" name="userName"/>
   <input type="submit" value="submit"/>
  </form>
 </body>
</html>
src/main/java/com/dotnet/HelloAction.java
package com.dotnet;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

@Action("/hello")
@Result(name = "success", location = "/results.jsp")
public class HelloAction
{
 public String execute()
 {
  System.out.println("User name: " + userName);
  return "success";
 }
 
 private String userName;

 public String getUserName() {
  return userName;
 }

 public void setUserName(String userName) {
  this.userName = userName;
 }
}
src/main/webapp/results.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html>
<html>

 <head>
  <title>Results page</title>
 </head>

 <body>
  <p>Hello <s:property value="userName"/>!</p>
 </body>
</html>
src/main/resources/struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>
 <package name="default" extends="struts-default">
  <action name="hello" class="com.dotnet.HelloAction" method="execute">
   <result name="success">/results.jsp</result>
  </action>
 </package>
</struts>

Step 1: Modify the code in HelloAction.java as follows:

Struts 2 ActionSupport with Examples

We have performed the following modifications:

  1. Imported the ActionSupport package.
  2. Removed the @Result annotations from the class.
  3. Inherited from the ActionSupport class.
  4. Modified the execute() function to authenticate a user. If the user name is “world”, the user is let through. Otherwise, an error is displayed.

Step 2: Modify struts.xml as follows:

Struts 2 ActionSupport with Examples

We have added an extra line that, in case of an error, directs the user to another webpage called error.jsp.

Step 3: Create a new file called error.jsp in the src/main/webapp directory. This file will be used when access is denied to the user. Add the following content to the file:

Struts 2 Action Support with Examples

Step 4: Compile and execute the program on the Tomcat server. The following page must open in your default web browser:

Struts 2 Action Support with Examples

Enter a name. In my case, I have entered World. This should show us a webpage as follows (because access is granted). The following webpage must load:

Struts 2 Action Support

Step 5: Go back to the index page (http://localhost:8080/strutex/index.html). This time, enter the name which does not match the access criteria. The following output must be displayed:

Struts 2 Action Support

Congratulations! You have successfully completed an application using ActionSupport in struts2.

The Complete Code
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.dotnet</groupId>
  <artifactId>strutex</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <dependencies>
   <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>6.1.2.1</version>
   </dependency>
   
   <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-convention-plugin</artifactId>
    <version>6.1.2.1</version>
   </dependency>
   
   <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts-taglib</artifactId>
    <version>1.3.10</version>
   </dependency>
   
   <dependency>
    <groupId>jakarta.servlet</groupId>
    <artifactId>jakarta.servlet-api</artifactId>
    <version>6.0.0</version>
   </dependency>
  </dependencies>
  
  <build>
   <plugins>
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
    	  <artifactId>maven-compiler-plugin</artifactId>
    	  <version>3.11.0</version>
    	  <configuration>
     <source>17</source>
     <target>17</target>
    </configuration>
    </plugin>
   </plugins>
  </build>
</project>
src/main/webapp/WEB-INF/web.xml
<?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"
xsi:schemalocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/web-app_3_0.xsd"
id="apache-struts-config-example"
version="3.0">

 <display-name>Struts Config XML</display-name>
 
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
 </filter>
 
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

</web-app>
src/main/webapp/index.html
<!DOCTYPE html>
<html>
 <head>
  <title>Example of Struts</title>
 </head>
 
 <body>
  <form action="hello.action">
   <p>What is your name?</p>
   <input type="text" name="userName"/>
   <input type="submit" value="submit"/>
  </form>
 </body>
</html>
src/main/java/com/dotnet/HelloAction.java
package com.dotnet;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ActionSupport;

@Action("/hello")
public class HelloAction extends ActionSupport
{
 public String execute()
 {
  if(userName.equalsIgnoreCase("world"))
   return SUCCESS;
  else
   return ERROR;
 }
 
 private String userName;

 public String getUserName() {
  return userName;
 }

 public void setUserName(String userName) {
  this.userName = userName;
 }
}
src/main/webapp/results.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html>
<html>

 <head>
  <title>Results page</title>
 </head>

 <body>
  <p>Hello <s:property value="userName"/>!</p>
 </body>
</html>
src/main/resources/struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>
 <package name="default" extends="struts-default">
  <action name="hello" class="com.dotnet.HelloAction" method="execute">
   <result name="success">/results.jsp</result>
   <result name="error">/error.jsp</result>
  </action>
 </package>
</struts>
src/main/webapp/error.jsp
<%@ page contentType = "text/html; charset = UTF-8" %>
<%@ taglib prefix = "s" uri = "/struts-tags" %>

<html>  
   <head>
      <title>Access Denied</title>
   </head>
   
   <body>
      You are not authorized to view this page.
   </body>
</html>

In the next article, I am going to discuss Struts 2 Interceptors with Examples. Here, in this article, I try to explain Struts 2 ActionSupport and I hope you enjoy this Struts 2 ActionSupport article.

Registration Open For New Online Training

Enhance Your Professional Journey with Our Upcoming Live Session. For complete information on Registration, Course Details, Syllabus, and to get the Zoom Credentials to attend the free live Demo Sessions, please click on the below links.

Leave a Reply

Your email address will not be published. Required fields are marked *