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.


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

</form-beans>

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

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

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

<action path="/searchSubmit"
type="org.springframework.web.struts.DelegatingActionProxy"
input="/searchEntry.do"
validate="true"
name="searchForm">
<forward name="success" path="/WEB-INF/pages/detail.jsp"/>
<forward name="failure" path="/WEB-INF/pages/search.jsp"/>
</action>

</action-mappings>

<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>


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


</struts-config>



The corresponding Spring bean configuration...


<beans>

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

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

<bean id="employeeService" class="struts.spring.employee.business.EmployeeImpl">
<property name="employees">
<map>
<entry key="1">
<ref bean="employee_1" />
</entry>
<entry key="2">
<ref bean="employee_2" />
</entry>
</map>
</property>
</bean>

<bean name="/searchSubmit"
class="struts.spring.employee.actions.FindEmployeeAction">
<property name="employeeService">
<ref bean="employeeService"/>
</property>
</bean>

</beans>



Merits:
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 : http://www.ibm.com/developerworks/java/library/j-sr2.html

6 comments:

shakti012 said...

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

Kudo said...

Hi,

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?

Thanx!

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...

GOOD........................................

dharmendra said...

Great Explanation. Another great article i recommend is:

this