Model View Controller 2 architecture

Spring Book – Chapter 12 – Spring and Struts Integration

The bridge between the Action in struts-config.xml and sample-servlet.xml is built with the action-mapping’s “path” and the bean’s “name”. If you have the following in your struts-config.xml file as shown below:

You must define that Action’s bean with the “/users” name in sample-servlet.xml as shown below:

Let’s now see the two options using the ContextLoaderPlugIn plug-in class in the following sections as detailed below.

Option 1 using DelegatingRequestProcessor

One of the easiest ways of using ContextLoaderPlugIn is to override the Struts RequestProcessor with Spring’s DelegatingRequestProcessor class. during startup Spring’s DelegatingRequestProcessor will lookup the Struts Actions defined in ContextLoaderPlugIn’s WebApplicationContext.

Defining the plug-in can be done either by using a single ContexLoaderPlugIn for all your Struts modules and then load the context in all your Struts modules or you can also define separate ContextLoaderPlugIn for each of your Struts modules keeping your modularization intact and then using the “contextConfigLocation” parameter to load the entire configuration file. To configure the DelegatingRequestProcessor in your Struts configuration file (struts-config.xml), override the “processorClass” property in the <controller> element as shown in Listing 12-5 below.

Listing 12-5. Configuring DelegatingRequestProcessor in struts configuration file

After adding this setting as shown in Listing 12-5 above, your Action will automatically be looked up in Spring’s context file, no matter what the type. We now need to register the action class in the Spring configuration file as shown in Listing 12-6 below.

Listing 12-6. Declaring action classes as Spring beans in the Spring configuration file

In Listing 12-6 above, we register Struts actions as Spring’s beans. The names of the beans must be same as in Struts configuration file, which will allow Spring to populate the beans at run time. In this approach the Spring beans are dependent on the RequestProcessor, this could reduces the flexibility of the application as a whole.

Option 2 using DelegatingActionProxy

Another approach or rather solution is to delegate Struts action management to the Spring Framework by registering a proxy in the Struts configuration file (struts-config.xml) in the action mapping section. This configured proxy will now be responsible for looking up the Struts action in the Spring context. In this approach the action is under Spring’s control and because of which it populates the action’s JavaBean properties and allows applying features such as Spring’s AOP interceptors resulting in more control and customization as a whole. The proxy can be declared in the “action-mappings” section of Struts configuration file as shown in Listing 12-7 below.

Listing 12-7. Configuring proxy in the Struts configuration file

The bean definitions in the Spring configuration file (sample-servlet.xml) remains the same in this approach as well.

Off the various approached discussed in this section, the action delegation method using DelegatingActionProxy is the best. In this approach, the Struts action has no knowledge of Spring and could be used in non-Spring applications without changing a single line of code.  Once you have your Struts action under Spring’s control, you can leverage Spring to give them more powerful features.

Page Visitors: 6406

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 *