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; http://struts.apache.org/2.0.8/docs/spring-plugin.html.
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 struts.properties as shown in Listing 12-8 below.
Listing 12-8. Configuring object factory in struts.properties file
1 |
struts.objectFactory = org.apache.struts2.spring.StrutsSpringObjectFactory |
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
1 2 3 4 5 6 7 |
<struts> <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" /> ... </struts> |
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 struts.properties file
1 |
struts.objectFactory.spring.autoWire = type |
The autowire property in the struts.properties file can be set to several options as detailed in the Table 12-1 below.
Table 12-1. Autowire options
Attribute | Description |
|
|
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
1 2 3 4 5 6 7 |
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener</listener-class> </listener> |
- 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
1 2 3 4 5 6 7 8 9 |
… <beans default-autowire="autodetect"> <bean id="someService"/> ... </beans> |
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.
Summary
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


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