Using Spring In A Struts 2 Web Application

Introduction

Integrating Spring into your Struts 2 web applications provides many benefits including:

  • Significantly reduces coupling between classes
  • Central management of class dependencies
  • Applications are easier to maintain and change
  • Leverage Spring features that Struts 2 doesn't provide
    • Aspect-oriented programming
    • Spring JDBC

In March 2009, I put together a class for our Java developers on using Struts 2 and Spring together. For the class I created two example applications. The instructions for where to download and how to install/run these example applications are at:

/spring/Struts2_Spring_Example Installation and Running Instructions.pdf

/spring/ContactsWebApp Installation and Running Instructions.pdf

Our development team uses MyEclipse as our Java IDE so the example applications can easily be imported into MyEclipse. Here is a link to a version that was created using plain Eclipse (with the maven 2 plugin).

Struts 2 and Spring Example Applications

The Struts2_Spring_Example application can be imported into any version of MyEclipse 6 or higher.  The jars needed for the application are included in the download (which is why the download is over 6 mb).  This is a simple application that demonstrates the basics of integrating Struts 2 and Spring.

The ContactsWebApp demonstrates how to integrate Spring into a Struts 2 web application and use additional Spring features such as aspect-oriented programming and Spring JDBC. You can view a working demo of the ContactsWebApp here.

To build the ContactsWebApp application you need MyEclipse 7 (which comes with Maven support).  This project uses Maven to manage the required jar files (thus the download is only 80 kb).  If you don't have MyEclipse 7, you can unzip the archived project file and view the sources files. You could then copy the source files into your own Java IDE.

The ContactsWebApp also uses an embedded Derby database.  The instructions explain how to get the database (it's a very small download) and where to save the database on your computer.  You don't need to configure anything else to use the embedded Derby database.

Struts 2 and Spring Presentation

You can also download a version of the class presentation at:

/spring/Using Struts 2 and Spring Frameworks Together.pdf

References:

  1. Spring Framework  - http://www.springsource.org/documentation
  2. Spring Community Forums - http://forum.springsource.org/
  3. Introduction to the Spring Framework 2.5   - http://www.theserverside.com/tt/articles/article.tss?l=IntrotoSpring25
  4. Spring in Action, 2nd Edition, Manning Publishing, August 2007
  5. Pro Spring 2.5, Apress Publishing, August 2008
  6. Struts 2 Framework - http://struts.apache.org
  7. Using Struts 2 and Spring Frameworks Together, Bruce Phillips Blog   - /blog/index.cfm/2008/10/17/Using-Struts-2-and-Spring-Frameworks-Together
  8. Struts 2 In Practice, Manning Publishing,  http://www.manning.com/wannemacher/
  9. Apache Struts 2 Documentation, Spring Plugin - http://struts.apache.org/2.x/docs/spring-plugin.html
  10. Struts User Forum - http://www.nabble.com/Struts---User-f206.html
  11. Maven - http://maven.apache.org/
  12. Maven - http://books.sonatype.com/maven-book/index.html

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Thanks! This is just what I was looking for.
# Posted By Steven Hunter | 5/11/09 1:55 PM
Thanks very much!!! This is a great tutorial! Now i've better understand the functionality and the advantages in integrating struts2 and spring!! Thank's a lot!!!
# Posted By Stefano Corallo | 5/30/09 4:49 AM
Bruce,
I am having problem with the spring request scoped beans in struts, I am doing exactly the same as in your example code (RequestContextListener, scope the bean ) but I am getting the error
Error 500:.. Unable to instantiate Action, Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException:.... RequestContextListener or RequestContextFilter to expose the current request.

I couldn't figure out why, I am using struts 2.1.6 and spring 2.5.3, wondering if I am missing out anything. Appreciate your thoughts!!
# Posted By raj | 6/22/09 2:47 PM
Raj - did you add the listener class to your web.xml:

<listener>
   <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
# Posted By Bruce | 6/22/09 3:06 PM
Two and a half years later and your example is still very helpful :).
Thanks a lot.
# Posted By Dejan Stolic | 8/10/11 5:42 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.1.002. Contact Blog Owner