Model View Controller 2 architecture

Spring Book – Chapter 12 – Spring and Struts Integration

Spring and Struts 2.x Integration

Struts 2 is effectively a different product – a successor of WebWork 2.2. Struts 2 is shipped with Spring Plugin for the built-in Spring integration.

Spring Plugin

The Spring Plugin works by overriding the Struts ObjectFactory to enhance the creation of core framework objects. When an object is to be created, it uses the class attribute in the Struts configuration to correspond to the id attribute in the Spring configuration. If not found, the class will try to be created as usual, and then be autowired by Spring.

The details provided in this section is as given in the Struts 2 Documentation specifically on Spring Plugin which you can find in the following URL;

To enable Spring integration, simply include struts2-spring-plugin-x-x-x.jar in your application. If you are using more than one object factory, (for example, by including both the Spring and Plexus plugins in your application,) you will need to set the struts.objectFactory property in as shown in Listing 12-8 below.

Listing 12-8. Configuring object factory in file

The object factory can also be configured in one of several XML files as shown in Listing 12-9 below.

Listing 12-9. Configuring object factory in struts.xml file

The framework enables “autowiring” by default. To change the wiring mode, modify the spring.autowireproperty as shown in Listing 12-10 below.

Listing 12-10. Configuring autowiring model in file

The autowire property in the file can be set to several options as detailed in the Table 12-1 below.

Table 12-1. Autowire options

Attribute Description
  1. name
  2. Auto-wire by matching the name of the bean in Spring with the name of the property in your action. This is the default
  3. type
  4. Auto-wire by looking for a bean registered with Spring of the same type as the property in your action. This requires you to have only one bean of this type registered with Spring
  5. auto
  6. Spring will attempt to auto-detect the best method for auto-wiring your action
  7. constructor
  8. Spring will auto-wire the parameters of the bean’s constructor

Having done the initial bit, if you would like to enable Spring Integration for other application objects, we need to do the following two steps as summarized below:

  • Configure Spring Listener – Configure the Spring listener in the web.xml file as shown in Listing 12-11 below.

Listing 12-11. Configuring Spring listener in web.xml file

  • Register your objects as beans in the Spring configuration file – register all objects required to be accessed by other application objects declare them as beans in the Spring configuration file as shown below in Listing 12-12.

Listing 12-12. Declaring Spring beans in Spring configuration file

Figure 12-4 below shows how your Struts 2 application along with the Spring Framework fits into your web application architecture.


Figure 12-4. Struts 2 along with Spring Framework fitting into your web application architecture

Spring AOP with Struts

After you have integrated Spring to your Struts application, you can now use the power of Spring to solve certain business cases in your Struts application. One such issue that we can tackle is to identify cross-cutting concerns and writing appropriate Spring interceptors to intercept these specific cross cutting concerns and avoid code tangling and scattering to a larger extent.

To use Spring AOP in your Struts application, you will have to do the following steps as detailed below:

  • Create Interceptor
  • Register the Interceptor
  • Declare the Interceptor

We will be covering Spring AOP in detail in Chapter 21, where will go into detail of each of the steps mentioned above. After integration of Spring with your Struts application you can use all the powerful feature which Spring provides out-of-box with ease. I just brought this concept here to just let you know that you can do such wonderful things after the integration.


Web application frameworks such as Struts solve common problems, so developers can focus on their application’s unique functionality. Struts make use of most of the standard Java servlet API and builds on the common design patterns, especially the MVC architectural paradigm. The framework encourages a “layered” design for applications helps making applications both robust and scalable.

You would have by now made yourself comfortable with the Spring Framework as a whole and would now admire the capabilities it gives to your application. Integrating Struts and Spring can be termed as marriage made in heaven and this Chapter let you do this.

This Chapter made a big assumption that the reader is conversant with the Struts Framework as whole and has got experience developing application using it. After reading through this small Chapter, you would be able to integrate web application based on Struts, both new and existing, with the Spring Framework.

Page Visitors: 6135

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 *