Thursday, 21 May 2009

Method C : Delegate Struts Action management to the Spring framework

Here we are going to delegate Struts action management to the Spring framework.

All you have to do is...

For each action that uses Spring, you need to define the action mapping to use org.springframework.web.struts.DelegatingActionProxy and declare a matching (action "path" == bean "name") Spring bean for the actual Struts action. This is an example of an action that requires an instance of UserDatabase:

Note the changes in struts-config file.

Inside action-mappings instead of declaring the action's class name, it registers the name of Spring's proxy class. The DelegatingActionProxy class uses the action mapping name to look up the action in the Spring context. This is the context that was declared with ContextLoaderPlugIn.

<form-bean name="searchForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="id" type="java.lang.Long"/>


<global-forwards type="org.apache.struts.action.ActionForward">
<forward name="welcome" path="/"/>
<forward name="searchEntry" path="/"/>
<forward name="searchSubmit" path="/"/>

<action path="/welcome" forward="/WEB-INF/pages/welcome.htm"/>

<action path="/searchEntry" forward="/WEB-INF/pages/search.jsp"/>

<action path="/searchSubmit"
<forward name="success" path="/WEB-INF/pages/detail.jsp"/>
<forward name="failure" path="/WEB-INF/pages/search.jsp"/>


<message-resources parameter="ApplicationResources"/>

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/beans.xml"/>


The corresponding Spring bean configuration...


<bean id="employee_1" class="struts.spring.employee.beans.Employee">
<property name="id" value="1" />
<property name="name" value="Sneha" />

<bean id="employee_2" class="struts.spring.employee.beans.Employee">
<property name="id" value="2" />
<property name="name" value="Prashant" />

<bean id="employeeService" class="">
<property name="employees">
<entry key="1">
<ref bean="employee_1" />
<entry key="2">
<ref bean="employee_2" />

<bean name="/searchSubmit"
<property name="employeeService">
<ref bean="employeeService"/>


1. As the action is under Spring's control, it populates the action's JavaBean properties and leaves the door open to applying features such as Spring's AOP interceptors.

The action-delegation solution is the best of the three.

Hope it will be useful...

References :


shakti012 said...

A nice tutorial. I was looking for ways to integrate Struts to Spring. Helpful indeed.

Kudo said...


can you post again plaease the source code of the Action class?

All the Action implementations I've found in articles about integrating Struts and Spring, seem to not take care of threading issues.

E.g. in the article from ibm that you referenciate, is having a global field like the
private BookService bookService;
a good idea?


Sandeep Gupta said...

Honestly, I did not read any of your articles but by looking at it, first thing came to my mind is to let you know about "SyntaxHighlighter 2.0", which makes code representable on the blog... use it and make more presentable..

YoungW21087 said...

Nice Post~!!!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Ko212ryS_Dahle0 said...


dharmendra said...

Great Explanation. Another great article i recommend is: