Apache Struts Hello World by 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" "http://struts.apache.org/dtds/struts-2.1.dtd">
<!-- config file for the Struts 2.0 Hello World Tutorial -->
<struts>
  <package name="default" extends="struts-default">
    <action 
      name="hello" 
      class="com.mcnz.struts.HelloWorldAction"
      method="execute">
        <result name="success">/results.jsp</result>
    </action>
  </package>
</struts>

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 -->
<html>
  <body>
    <form action="hello.action">
      Please tell my your name
      <input name="firstName" type="text" value="World"/>
      <input value="submit" type="submit" />
    </form>
  </body>
</html>

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.

Apache and Docker Tutorials

Master the fundamentals of Apache, Docker and Kubernetes technology.

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" %>
<html>
  <body>
    <em>Struts Hello World Example Results Page</em>
    <p>Hello <s:property value="firstName"/> ! </p>
  </body>
</html>

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="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>Hello World in Struts 2.0</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 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="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>v.2 Hello World Struts Example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <!-- Hello World in Struts with a struts.xml file -->
  <dependencies>
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>2.5.5</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-convention-plugin</artifactId>
      <version>2.5.10</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>

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.

 

 

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close