Spring and Struts Integration
Package org.springframework.web.struts provides support classes for integrating a Struts web tier with a Spring middle tier which is typically hosted in a Spring root WebApplicationContext.
Spring and Struts 1.x Integration
The Spring architecture allows you to connect Struts as your Web framework to Spring-based business and persistence layers. Two important methods by which we can achieve integration of your Struts 1.x application with Spring are as summarized below:
- Using Spring’s ActionSupport class
- Using Spring’s ContextLoaderPlugin class
Note: As of Spring 3.0 many of the classes used for achieving the integration of Struts 1.x with Spring has become deprecated and more focus now lies for supporting the new flavor of Struts namely Struts 2.
The following section will let you do integration of Struts application with the Spring Framework.
Using Spring’s ActionSupport Classes
In this type of integration you extend your Action class from the Spring’s org.springframework.web.struts.ActionSupport class instead of the default Struts Action class as shown in Listing 12-1 below.
Listing 12-1. Struts Action class extending Spring’s ActionSupport class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import java.io.IOException; import javax.servlet.*; import org.apache.struts.action.*; import org.springframework.context.ApplicationContext; import org.springframework.web.struts.ActionSupport; public class CustomerSearch extends ActionSupport { public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { //… ApplicationContext ctx = getWebApplicationContext(); MySampleService mySampleService = (MySampleService) ctx.getBean("MySampleService"); //… return mapping.findForward("success"); } } |
The Spring’s ActionSupport class provides you with the method getWebApplicationContext() to obtain the Spring’s ApplicationContext and from the ApplicationContext you can access the business service layer as required by your application requirement as shown in Listing 12- 2 below.
Listing 12-2. Getting the ApplicationContext in the Action class
1 2 3 4 5 |
ApplicationContext ctx = getWebApplicationContext(); MySampleService mySampleService = (MySampleService) ctx.getBean("MySampleService"); mySampleService.callServiceMethod(); |
Spring includes subclasses for all of the standard Struts Action class by merely having a corresponding Spring version with the string “Support” appended to the name. Please note that all the below classes are deprecated as of Spring 3.0. They are:
- org.springframework.web.struts.ActionSupport
- org.springframework.web.struts.DispatchActionSupport
- org.springframework.web.struts.LookupDispatchActionSupport
- org.springframework.web.struts.MappingDispatchActionSupport
This approach involves changes in the java files of your application, which is not a suggested way of doing things. If due to any reason you would like to take the Spring away from your Struts application, you will have to redo the entire changes and this can be over the period of the project can be quite exhaustive. Also by doing this, Struts actions are not in control of the Spring Framework.
Using Spring’s ContextLoadedPlugin Class
The class org.springframework.web.struts.ContextLoaderPlugin is provided with Spring’s 1.0.1 release. This plug-in loads the Spring application context for Struts application’s ActionServlet class. This context refers to the root org.springframework.web.context.WebApplicationContext loaded by the org.springframework.web.context.ContextLoaderListener as its parent.
The default name of the context file is the name of the mapped servlet appended with “-servlet.xml”. If ActionServlet is defined in web.xml as <servlet-name>sample</servlet-name>, the default is /WEB-INF/sample-servlet.xml. To configure this plug-in, add the following XML to the plug-ins section near the bottom of your struts-config.xml file as shown in Listing 12-3 as below.
Listing 12-3. Configuring plug-in in struts-config.xml file
1 |
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"/> |
The location of the context configuration files can be customized using the “contextConfigLocation” property in the struts-config.xml as shown in Listing 12-4 below.
Listing 12-4. Configuring plug-in using custom context configuration file in struts-config.xml file
1 2 3 4 5 6 7 |
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/sample-servlet.xml,/WEB-INF/applicationContext.xml"/> </plug-in> |
By loading all configuration files as shown in Listing 12-4 above will be useful when using struts testing tools like StrutsTestCase.
After configuring this plug-in in struts configuration file as shown above, you can configure your Action to be managed by Spring. There are two methods of integrating Struts with Spring using this plug-in and as summarized below:
- Option 1 – Overriding the Struts RequestProcessor with Spring’s org.springframework.web.struts.DelegatingRequestProcessor.
- Option 2 – Delegate Struts action management to the Spring Framework.
Page Visitors: 6135

Tomcy John

Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023
“Maintain the excellent job mate. This web blog publish shows how well you comprehend and know this subject.”
Thanks mate.
Hii,
Nice explaination thank you.
Thanks, nice post