Get started Bring yourself up to speed with our introductory content.

The Apache Struts Hello World example

I recently published an article about how to build a zero-configuration Struts Hello World example app with annotations instead of a Struts configuration file. In this Struts tutorial, I’d like to demonstrate how to create the same application but without the annotations. In this case, the Struts 2.0 Hello World app will indeed use a file names struts.xml for configuration.

In this Struts Hello World example a web page will submit an HTML form to a server-side target named hello.action. This action will be implemented by a Struts action class named HelloWorldAction. When the execute method of the HelloWorldAction class completes successfully, a file named results.jsp will be invoked to render a result for the client. Since we will not use annotations in this Struts Hello World example, this information must all be captures through xml. The struts.xml file that implements this functionality looks as follows:

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts 
Configuration 2.1//EN" "">
<!-- config file for the Struts 2.0 Hello World Tutorial -->
  <package name="default" extends="struts-default">
        <result name="success">/results.jsp</result>

The input form

The file that kicks off the web based request-response cycle is named index.html and is coded as follows:

<!-- Hello World in Struts index.html file -->
    <form action="hello.action">
      Please tell my your name
      <input name="firstName" type="text" value="World"/>
      <input value="submit" type="submit" />

Notice the target of the form is hello.action, which maps to the action named hello in the struts.xml configuration file. Furthermore, the input text field named firstName will be mapped to a property in the HelloWorldAction class that follows.

Learn Apache Struts 2 Quickly

Here’s how to learn the fundamentals of Struts 2.5:

Follow these steps and you’ll be an Apache Struts 2 expert in no time!

Struts Hello World action class

The action class which handles the incoming request is coded as follows:

package com.mcnz.struts;
/* Hello World in Struts 2 Tutorial Action Class */
public class HelloWorldAction {

  public String execute() {
    System.out.print("Value of firstname is: " + firstName);
    return "success";

  private String firstName;

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

The Struts view component

The results.jsp file acts as the view on this Struts Hello World example. The JSP pulls the name from the action class through the use of a Struts custom tag. The result is a page that says Hello to the user, based on the name they typed into the text field.

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html; charset=UTF-8" %>
    <em>Struts Hello World Example Results Page</em>
    <p>Hello <s:property value="firstName"/> ! </p>

Struts 2.5 web configuration

The above pieces of code represent the various Struts components the application requires. However, for Struts to work, the StrutsPrepareAndExecuteFilter must be configured in the web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" 
  <display-name>Hello World in Struts 2.0</display-name>



Struts and Maven

Finally, the required Struts libraries must be made available to the application in order for it to compile and run. The following POM file represents the Java 1.8 configuration used to compile and run this Struts Hello World example with version 2.5 binaries:

<project xmlns=""
  <artifactId>v.2 Hello World Struts Example</artifactId>
  <!-- Hello World in Struts with a struts.xml file -->



With all of these properties configured, the Struts Hello World example will run on any Java applications server that supports the Servlet and JSP API, including popular open source servers such as Tomcat or Jetty.

Steps to create a Hello Worlds Struts 2 app

In review, the steps to create the Struts Hello World example are:

  1. Create a standard Java web project
  2. Create an HTML page that calls a Struts action class
  3. Create a Struts Action class controller to handle the incoming request
  4. Code a JSP to act as the view component which sends HTML to the client
  5. If you are not using annotations, add a struts.xml file to the classpath
    1. The struts configuration file should preferably reside in the resources folder
    2. Map the Struts action to the Java class and its various view JSPs
  6. Add a reference to the StrutsPrepareAndExecuteFilter in the web.xml file
  7. Make the required binaries available by providing a properly configured Gradle or Maven build file
  8. Run your Struts Hello World application

And that’s how easy it is to create a simple Hello World Struts application that can run on a modern Java web server.



Join the conversation

1 comment

Send me notifications when other members comment.

Please create a username to comment.

Will partners have to branch out into multiple cloud provider alliances to survive?