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.


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" 
  <display-name>Example Not Struts Config XML</display-name>



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:

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

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.*;

@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" %>
  Results page
  Hello <s:property value="firstName"/> !!!

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:


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"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <!-- Example Struts App with no Struts Config XML file -->







App Architecture
Software Quality
Cloud Computing