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:
1 |
<action path="/users" .../> |
You must define that Action’s bean with the “/users” name in sample-servlet.xml as shown below:
1 |
<bean name="/users" .../> |
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
1 2 3 4 5 6 7 |
<controller> <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/> </controller> |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<beans> <bean id="serviceClassRef"/> <bean name="/sampleSearch"> <property name="serviceClass"> <ref bean="serviceClassRef"/> </property> </bean> </beans> |
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
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 |
… <action-mappings> <action path="/welcome" forward="/WEB-INF/views/start.htm"/> <action path="/sampleSearch" forward="/WEB-INF/views/sampleSearch.jsp"/> <action path="/submitSearch" type="org.springframework.web.struts.DelegatingActionProxy" input="/sampleSearch.do" validate="true" name="sampleSearchForm"> <forward name="success" path="/WEB-INF/views/detail.jsp"/> <forward name="failure" path="/WEB-INF/views/failure.jsp"/> </action> </action-mappings> … |
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: 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