Spring Framework Tutorial

Software Setup and Configuration (STS/Eclipse/IntelliJ)

Core Spring

Spring Annotations

Spring Data

Spring JDBC

Spring Security

How to Create a Project using Spring and Struts 2?

Combining Spring and Struts 2 in a single project allows you to leverage the strengths of both frameworks. While Spring provides comprehensive inversion of control (IoC) and dependency injection features, Struts 2 offers a robust MVC framework for web applications. Here's a step-by-step guide on setting up a project with Spring and Struts 2:

1. Set Up a New Maven Project:

Start by creating a new Maven project in your preferred IDE (Eclipse, IntelliJ, etc.):

  • Group ID: com.example
  • Artifact ID: spring-struts2-demo

2. Add Dependencies in pom.xml:

You'll need to add dependencies for both Struts 2 and Spring. Here's a sample set of dependencies to get started:

<dependencies>
    <!-- Spring Core & MVC -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.x.x.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.x.x.RELEASE</version>
    </dependency>

    <!-- Struts 2 -->
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-core</artifactId>
        <version>2.x.x</version>
    </dependency>

    <!-- Struts 2 Spring Plugin: To integrate Struts 2 with Spring -->
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-spring-plugin</artifactId>
        <version>2.x.x</version>
    </dependency>
</dependencies>

(Note: Replace x.x.x with the actual version numbers based on your requirements.)

3. Set Up Spring Configuration:

Create an XML file named spring-config.xml in the src/main/resources directory:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- Define your Spring beans here -->

</beans>

4. Configure Struts 2 with Spring:

Modify the struts.xml file to integrate with Spring:

<struts>
    <constant name="struts.objectFactory" value="spring" />
    <constant name="struts.objectFactory.spring.autoWire" value="name" />

    <!-- Define your Struts 2 actions and results here -->

</struts>

With the above configuration, Struts 2 will use Spring to instantiate and inject action classes.

5. Set Up Web Configuration:

Add/update your web.xml in src/main/webapp/WEB-INF:

<web-app>
    <!-- Struts 2 Configuration -->
    <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>

    <!-- Spring Configuration -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-config.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

6. Implement Your Application:

With the foundational setup ready, you can now begin implementing your Struts 2 actions, Spring services, DAOs, etc.

When defining a Struts 2 action bean in the Spring context (spring-config.xml), ensure that the bean name matches the class name (starting with a lowercase letter) so that Struts 2 can correctly pick it up and apply dependency injection.

Finally, while integrating Spring with Struts 2 can bring combined benefits, it's essential to recognize that there's an overhead of managing two frameworks. Ensure that the benefits you're seeking can't be achieved with a single framework before deciding to combine them.