eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

 

1. Introduction

Apache Struts 2 is an MVC-based framework for developing enterprise Java web applications. It is a complete rewrite of original Struts framework. It has an open source API implementation and a rich feature set.

In this tutorial, we will have a beginner’s introduction to different core components of the Struts2 framework. Moreover, we will show how to use them.

2. Overview of Struts 2 Framework

Some of Struts 2 features are:

  • POJO (plain old Java Objects)-based actions
  • plugin support for REST, AJAX, Hibernate, Spring, etc
  • convention over configuration
  • support of various view-layer technologies
  • ease of profiling and debugging

2.1. Different Components of Struts2

Struts2 is an MVC-based framework so the following three components will be present in all Struts2 applications:

  1. Action class – which is a POJO class (POJO means it is not part of any type hierarchy and can be used as a standalone class); we will implement our business logic here
  2. Controller – in Struts2, HTTP filters are used as controllers; they basically perform tasks like intercepting and validating requests/responses
  3. View – is used for presenting processed data; it is usually a JSP file

3. Designing Our Application

Let’s proceed with the development of our web app. It is an application where a user selects a particular Car brand and gets greeted by a customized message.

3.1. Maven Dependencies

Let’s add following entries to the pom.xml:

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.5.10</version>
</dependency>
<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-junit-plugin</artifactId>
    <version>2.5.10</version>
</dependency>
<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-convention-plugin</artifactId>
    <version>2.5.10</version>
</dependency>

The latest version of the dependencies can be found here.

3.2. Business Logic

Let’s create an action class CarAction which returns a message for a particular input value. The CarAction has two fields – carName (used for storing the input from the user) and carMessage (used for storing the custom message to be displayed):

public class CarAction {

    private String carName;
    private String carMessage;
    private CarMessageService carMessageService = new CarMessageService();
    
    public String execute() {
        this.setCarMessage(this.carMessageService.getMessage(carName));
        return "success";
    }
    
    // getters and setters
}

The CarAction class uses CarMessageService which provides the customized message for a Car brand:

public class CarMessageService {

    public String getMessage(String carName) {
        if (carName.equalsIgnoreCase("ferrari")){
            return "Ferrari Fan!";
        }
        else if (carName.equalsIgnoreCase("bmw")){
            return "BMW Fan!";
        }
        else {
            return "please choose ferrari Or bmw";
        }
    }
}

3.3. Accepting User Input

Let’s add a JSP which is an entry point in our application. This is a content of the input.jsp file:

<body>
    <form method="get" action="/struts2/tutorial/car.action">
        <p>Welcome to Baeldung Struts 2 app</p>
        <p>Which car do you like !!</p>
        <p>Please choose ferrari or bmw</p>
        <select name="carName">
            <option value="Ferrari" label="ferrari" />
            <option value="BMW" label="bmw" />
         </select>
        <input type="submit" value="Enter!" />
    </form>
</body>

The <form> tag specifies the action (in our case it is a HTTP URI to which GET request has to be sent).

3.4. The Controller Part

StrutsPrepareAndExecuteFilter is the controller, which will intercept all incoming requests. We need to register the following filter in the web.xml:

<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>

StrutsPrepareAndExecuteFilter will filter every incoming request as we are specifying URL matching wildcard <url-pattern>/*</url-pattern>

3.5. Configuring the Application

Let’s add following annotations to our action class Car:

@Namespace("/tutorial")
@Action("/car")
@Result(name = "success", location = "/result.jsp")

Let’s understand the logic of this annotations. The @Namespace is used for logical separation of request URI for different action classes; we need to include this value in our request.

Furthermore, the @Action tells the actual end point of the request URI which will hit our Action class. The action class consults CarMessageService and initializes the value of another member variable carMessage. After execute() method returns a value, “success” in our case, it matches that value to invoke result.jsp

Finally, the @Result has two parameters. First one, name, specifies the value which our Action class will return; this value is returned from the execute() method of Action class. This is the default method name which will be executed.

The second part, location, tells which is the file to be referred to after the execute() method has returned a value. Here, we are specifying that when execute() returns a String with value “success“, we have to forward the request to result.jsp.

The same configuration can be achieved by providing XML configuration file:

<struts>
    <package name="tutorial" extends="struts-default" namespace="/tutorial">
        <action name="car" class="com.baeldung.struts.CarAction" method="execute">
            <result name="success">/result.jsp</result>
        </action>
    </package>
</struts>

3.6. The View

This is the content of result.jsp which will be used for presenting the message to the user:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<body>
    <p> Hello Baeldung User </p>
    <p>You are a <s:property value="carMessage"/></p>
</body>

There are two important things to notice here:

  • in <@taglib prefix=”s” uri=”/struts-tags %> we are importing struts-tags library
  • in <s:property value=”carMessage”/> we are using struts-tags library to print the value of a property carMessage

4. Running the Application

This web app can be run in any web container, for example in Apache Tomcat. These are the required steps for accomplishing it:

  1. After deploying the web app, open the browser and access the following URL: http://www.localhost.com:8080/MyStrutsApp/input.jsp
  2. Select one of the two options and submit the request
  3. You will be forwarded to the result.jsp page with customized message based on the selected input option

5. Conclusion

In this tutorial, we walked through a step by step guide, how to create our first Struts2 web application. We covered different MVC related aspects in the Struts2 domain and showed how to put them together for development.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)