VOOZH about

URL: https://www.geeksforgeeks.org/java/how-to-create-a-project-using-spring-and-struts-2/

⇱ How to Create a Project using Spring and Struts 2? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Create a Project using Spring and Struts 2?

Last Updated : 23 Jul, 2025

Prerequisites:

In this article, we will discuss how the Spring framework can be integrated with the Struts2 framework to build a robust Java web application. Here I am going to assume that you know about Spring and Struts2 framework. The first question that will come to your mind is why should you integrate these two frameworks.

So let's first discuss a little bit about Spring as well as Struts 2 framework and why we should integrate these two frameworks. As we all know, the Spring framework is used to develop enterprise web applications and The Struts2 framework is used to develop MVC (Model View Controller) based web applications. Both Spring & Struts2 uses MVC architecture for building Java web applications, and Struts2 is based on Java Servlet API. Therefore, When we integrate Spring and Struts2 framework then MVC functionality should not overlap. Instead, they should supplement each other. As we all know there is a very cool feature of Spring i.e. dependency injection management. So we can use it to manage business beans and Struts’ action beans. On the other side, We'll be able to focus on our business logic implementation with Struts2 MVC architecture. This is a typical scenario for integrating Spring and Struts together, especially for legacy applications based on Struts.

How does Spring and Struts 2 integration work?

To integrate Spring and Struts 2 framework, we just have to add one jar file i.e. struts2-spring-plugin in the project's classpath which is provided by the Struts 2 framework. With the Spring plugin enabled, the Spring framework manages all Struts’ action classes as well as other beans (business classed, DAO classes, etc) via its inversion of control container (declared in Spring’s application context configuration file).

You guys must be thinking about how much theory to read. If something happens in practice, then it will be fun. So let's start the implementation of integration of Spring and Struts2 framework to develop a web application with login functionality. We will go step by step like below :

  1. Create a maven project & convert it to a Dynamic Web Project
  2. Add required Spring, Struts2, Java Servlet & JSP dependencies in pom.xml
  3. Add the Spring plugin i.e. struts2-spring-plugin dependency in pom.xml

As we discussed earlier, we are going to implement only login functionality in the project to demonstrate the Spring and Struts2 integration. So, here we will create a User model class, UserDAO class, and UserAction class to process the login request.

Now comes the View part of MVC architecture. Here we will create three JSP pages i.e. LoginForm.jsp, LoginSuccess.jsp & LoginError.jsp 

LoginForm.jsp

LoginSuccess.jsp

LoginError.jsp

Configuring Spring and Struts in web.xml. This part is the most important because of this configuration only the Spring and Struts 2 frameworks interact with each other. Spring acts as the dependency container and Struts acts as the MVC framework.

Next, we configure how Struts processes the workflow of the application. Create an XML file named struts.xml under the project’s source folder with the following code:

At last, we will create an XML file name appContext.xml under the /WEB-INF/spring directory where we define the beans which will be managed by the Spring IOC container by reading the appContext.xml file.

Finally, the project is ready after a lot of hard work. The project structure will look like below:

👁 project structure
 

To test the application, we have to integrate the apache tomcat server into IDE and run this application on the server. You don't have to worry about that now. I will test the application on your behalf. I will demonstrate each screen one by one. So after deploying the project into the apache tomcat server, we have to hit the browser with the URL below to get the login screen

http://localhost:8080/SpringStruts2Integration/LoginForm.jsp

LoginForm.jsp Output:

👁 LoginForm.jsp
 

Now we can test the login functionality by passing the correct and wrong credentials. So I will first pass the correct and then wrong credentials and display you the response page in both scenarios.

👁 LoginForm.jsp
 

LoginSuccess.jsp Output:

👁 LoginSuccess.jsp
 

LoginError.jsp Output:

👁 LoginError.jsp
 

In this tutorial, we reviewed how to use the Struts 2 Spring plugin to integrate Spring and Struts. By using the Struts 2 Spring plugin you can have Spring manage the dependencies of your ActionSupport classes. Of course, you can also take advantage of the many other benefits (AOP, Spring JDBC) that the Spring framework provides.

Comment