Struts 2 + Spring Integration Example

Struts 2 provides plug-ins for integration to any other frameworks. If you are using Spring for your bean management, then it is very easy for you to integrate Spring container to Struts 2 application. Spring can manage the beans and struts actions classes as beans. You have to just specify the bean name in the struts configuration file. The following steps to be taken for the Struts 2 and Spring integration.

Download the Struts2-Spring-plugin.jar. It is the library which integrates both the frameworks.

Add Spring libraries into the lib folder

Add context loader in the web.xml for loading the spring configuration file.

The above steps are the additional tasks which you have to do with the Struts 2 application for enabling the integration. This tutorial shows a very simple example for the integration. Lets look at the example code.

1. Action Class and Bean

Write a action class and declare a bean which will be injected from the spring container with default values.

2. Spring Configuration File

It is the traditional spring configuration file for declaring the spring beans. Note that here I have declared Struts action class as the spring beans and injected UserDetails beans to the action class.

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.