Model View Controller 2 architecture

Spring Book – Chapter 12 – Spring and Struts Integration

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

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

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

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

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: 6432

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

4 thoughts on “Spring Book – Chapter 12 – Spring and Struts Integration

Leave a Reply

Your email address will not be published. Required fields are marked *