you're reading...


How to integrate Spring Framework with a Struts2 application

Integrating Spring with your web application

Using Spring Framework facilitates good, extensible design by promoting interface driven programming, ease of testing through Inversion of Control and supporting various test frameworks and much more. Integrating Spring in a Struts2 based web application is a logical next step. In this post, we will integrate the Spring Framework in the Struts2 web application started in this post – Starting Struts2 web application development (using Maven2 and Eclipse)

Integrating Spring with the existing Struts2 application is pretty straight forward. We need the following modifications to the existing project to enable Spring support.

  1. Make sure that the struts2-spring-plugin is included as a dependency in your pom.xml

  2. Add Spring’s ApplicationContext to configure and manage Struts2 Action beans and/or any other beans used in the application.

    			<?xml version = "1.0" encoding="UTF-8"?>
    			<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
    				<!-- Example of Struts2 action instantiated by Spring -->
    				<bean id="helloAction" class="com.rajandesai.example.webapp.action.HelloAction" singleton="false" />
  3. Modify web.xml to add spring support. Changes are highlighted in the following code.

    <?xml version="1.0" encoding="UTF-8"?>

    <web-app id="web-app" version="2.4"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Simple Hello World App using Struts2 and Spring</display-name>

    <!– Listeners –>

  4. Modify struts.xml to use reference to the helloAction bean defined in the applicationContext.xml. We will also change the struts.objectFactory property to use spring object factory instead of struts.

    			<?xml version="1.0" encoding="UTF-8"?>
    			<!DOCTYPE struts PUBLIC
    			          "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"       
    			    <include file="struts-default.xml"/>
    				<constant name="struts.enable.DynamicMethodInvocation" value="false" />
    				<constant name="struts.devMode" value="true" />
    			    <constant name="struts.objectFactory" value="spring" />
    				<package name="helloworld" namespace="/hello" extends="struts-default">
    			        <default-interceptor-ref name="defaultStack"/>
    					<action name="hello" class="helloAction">
    					<!-- Add additional "example" package actions here. -->
    				<!-- Add addition packages and configuration here. -->
  5. We are now ready to deploy and test the application. As before, run mvn jetty:run from the project directory (where pom.xml exists).
    And then in the browser, try these sample URLs to test the application:

    1. Hello World: http://localhost:8080/hello/hello.action
      Boring Hello World
    2. Hello World with the twist: http://localhost:8080/hello/hello.action?name=John%20Doe&zip=02451&age=30&gender=M
      Twisted Hello World


    Integrating Spring with the Struts2 application is very straight forward. We are now ready to explore advantages Spring brings to the application design. Next, we will integrate Hibernate with this application and also see Spring simplifies writing unit tests for Struts2 actions.
    Source code is available here.


No comments yet.

Post a Comment