Apache Struts 2.5 with no Struts config XML file example

Java web frameworks popular at the turn of the century are often slagged for their reliance on cumbersome XML files for even the most basic configurations. Struts certainly falls into that category, as problems with the struts config XML file has undoubtedly thwarted the desires of many developers to learn and adopt the technology. But plenty has changed since the project’s initial release twenty years ago. In this Apache Struts 2.5 example, I’ll show you just how easy it is to create a zero configuration web application with the latest version of the Apache Struts framework. Believe it or not, but it’s easy to create an Apache Struts application without a Struts config XML file.

Struts 2.5 Maven configuration

To work with Struts, you need to obtain the required JAR files. In this Apache Struts 2.5 example we will use Maven to pull in the required JAR files. At the bare minimum you will require the Struts core, Struts convention and a reference to the Servlet and JSP APIs.

<dependencies>
  <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.5.5</version>
  </dependency>
  <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-convention-plugin</artifactId>
    <version>2.5.10</version>
  </dependency>
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
  </dependency>
</dependencies>

Struts 2.5 web.xml configuration

To get this Apache Struts example to handle HTTP based request, a filter entry must be added to the deployment descriptor of the Jakarta EE web project.

Apache and Docker Tutorials

Master the fundamentals of Apache, Docker and Kubernetes technology.

Here is the updated web.xml file in its entirety:

<?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/javaee/web-app_3_0.xsd" 
id="apache-struts-config-example" 
version="3.0">
  <display-name>Example Not 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>

Struts flow of execution

The Struts flow of execution for any web based request-response cycle follows the same basic pattern:

  • Form data is sent to the server through a web based HTML form
  • The request is mapped to a Struts action class
  • Fields in the HTML form are mapped to properties in the action class
  • The execute method of the action class is invoked
  • A web page is invoked to render the response for the client

In this Apache Struts example the following components will be used to implement the Struts flow:

  • A file named index.jsp which contains the HTML form and an input box named firstName
  • A Java class named HelloAction with a property named firstName and an execute method
  • A file named results.jsp that includes the firstName property in the response it renders back to the client

The Struts HTML input form

There are three key elements to the index.jsp that allow the server-side Apache Struts components to process the web request:

  • The HTML form used to send user input to the server
  • The action of the form that references hello.action
  • The input field assigned the name value of firstname

Here is the full code for the input.jsp:

<html>
  <body>
  <form action="hello.action">
    May I ask your name? 
    <input type="text" name="firstName"/>
    <input type="submit" value="submit"/>
  </form>
  </body>
</html>

The Struts Action class

The HTML form invokes a server-side resource named hello.action. This component is embodied in source code through the class named HelloAction.java. Notice the class includes an @Action annotation which defines its name, and a @Result annotation that indicates the results.jsp file should be used to render a response to the client if the execute method runs successfully. There is also a property named firstName which maps to the input field in the HTML form with the same name.

package com.mcnz.struts;
import org.apache.struts2.convention.annotation.*;

@Action("/hello")
@Result(name = "success", location = "/results.jsp")
public class HelloAction { 
  private String firstName;

 public String execute() {
  System.out.println("Inside execute of hello.action");
  System.out.println("Value of firstName is: " + firstName);
  return "success";
 }

 public String getFirstName() {
  return firstName;
 }
 public void setFirstName(String fn) {
  this.firstName = fn;
 }

}

Historically, the Struts action and result mapping would be written in a Struts config XML file. However, in this modernized Apache Struts example the use of annotations remove the need to include any XML based configuration for Struts actions.

The Struts result page

The Struts action class gets passed to the JSP responsible for rendering a result to the client. This allows the results.jsp file to access any properties defined in the HelloAction class through the use of Struts tags. The name submitted through the form is rendered back to the client, along with a little hello message, through the inclusion of Struts tags in the markup. The result.jsp looks as follows:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
  Results page
  <br/>
  Hello <s:property value="firstName"/> !!!
</body>
</html>

With the required files created and coded, the Apache Struts example can be packaged as file named struts-example.war and deployed to any Java applications server such as Tomcat or Jetty. The URL to access the index.jsp file will take the form:

http://localhost:8080/struts-example/index.jsp

How to write an Apache Struts example

In review, the steps to code a simple Apache Struts 2 example without a Struts config XML file are:

  • Include the required JAR file dependencies to your web project
  • Add a reference to the StrutsPrepareAndExecuteFilter to the web.xml file
  • Create an standard web page with an input form and a reference to a Struts action
  • Code a Struts Action class with an execute method to handle the HTML form submission
  • Create a JSP that can render a response when the Struts action indicates execution was a success

And that’s all there is to creating an Apache Struts example application that does not require a Struts config XML file.

Full Apache Struts 2.5 example Maven file

Only the partial Maven POM file was listed above. Here is the complete file, along with a configuration setting to force JDK 1.8 compliance.

<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.mcnz</groupId>
  <artifactId>struts-example-no-struts-config-xml</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <!-- Example Struts App with no Struts Config XML file -->

  <dependencies>
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>2.5.5</version>
    </dependency>
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-convention-plugin</artifactId>
      <version>2.5.10</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>