Introduction to Apache Struts 1

Even though Apache Struts version 1.x has been deprecated for a long time, I still decided to write a tutorial about Apache Struts 1. That’s because, maybe some of you, when working with customers whose systems have been around for a long time and they are using Struts 1, there will be a need to find out what Struts 1 is, how it works to understand the customer system. In this tutorial, I will introduce you to some of the basics of Struts 1 and make a small example to see how Struts 1 works!

The first thing you need to know about Apache Struts is … it is an open-source framework using the Java Servlet API to help you implement Java web applications according to the MVC (Model-View-Controller) model. It provides us with all the components needed to build a web application including web form components, validator, multi-language, error handling.

The last version of Apache Struts 1 is 1.3.10 and you can use Java 8 to run Apache Struts 1 application.

As an example, I will create a new Jakarta EE using Maven in Eclipse as follows:

The contents of my pom.xml file are as follows:

In addition to Apache Struts 1 dependencies, I also declare to use Java Servlet API to work with Java Servlet.

I also use the Jetty server plugin version 9.x to run Java web application using Java version 8!

Similar to other Java web frameworks like Spring MVC’s DispatcherServlet, Apache Struts 1 also defines a Java Servlet class named ActionServlet that extends from Java EE’s HttpServlet class to act as the front controller handling all requests to the application.

We need to declare this ActionServlet in the project’s web.xml file to register it with the Server runtime.

For example, I declare the following:

Here, I am configuring so that all request URLs ending with .do extension will be handled by ActionServlet of Apache Struts 1. This is the extension suggested by Apache Struts 1!

In order for the ActionServlet class to handle requests from users, we need to define this ActionServlet class to know which controller will handle which request so that when it receives a user’s request, it will forward the request to that controller properly.

You can define this information in the struts-config.xml file in the src/main/webapp/WEB-INF directory:

then configure the ActionServlet to use this file as follows in the web.xml file:

Before I talk about the contents of the struts-config.xml file, I need to tell you a few things first:

First, in Apache Struts 1, you define the controller by creating a new class that extends its Action class.

For example, I define a class HelloWorldAction as follows:

You need to implement the execute() method of the Action class to add code to handle business logic before forwarding the request to the page displaying the results. In the struts-config.xml file, we will define which request is handled by this HelloWorldAction controller.

Second, in Apache Struts 1, the ActionForm class acts as the Model in the MVC pattern. It is a JavaBean class that comes with an Action containing information to display in the View!

In the above execute() method, you can define a HelloWorldForm class to define a “Hello World from Huong Dan Java” message as follows:

Use this HelloWorldForm class to set the message “Hello World from Huong Dan Java” in the HelloWorldAction class as follows:

After you have set the data to the Model, you need to select View to return the results to the user. We will define this View in the file struts-config.xml!

The content of the file struts-config.xml, I define as follows:

The configuration information for Apache Struts 1 is located in the <struts-config> tag. You use the <action-mapping> tag to map the request and the controller handle that request. As you can see above, I am defining an action with the path “/helloworld” which is handled by a controller with type HelloWorldAction. The name attribute is the name of the ActionForm defined with this request. Inside the <action> tag, I also define a <forward> tag with the name “success” to specify that after successful processing, the View “/helloworld.jsp” will be returned.

The ActionForm you define in the <form-beans> tag as I did above!

The file View helloworld.jsp has the following content:

I am using taglib <bean> of Apache Struts 1 to display the content of the message property of Model HelloWorldForm. Depending on your needs and website content, you can use other taglibs Apache Struts 1. Details of the taglibs that Apache Struts 1 supports, you can find here.

Now you can use the ActionMapping class to select the View that will be returned to the user as follows:

At this point, you can run the application with the Maven Jetty plugin and go to the address http://localhost:8080/helloworld.do, you will see the following results:

All requests end with the .do extension will be handled by Apache Struts 1 and return the results as you can see!

5/5 - (1 vote)

Add Comment