Thursday, 21 May 2009

Method A : Using Spring's ActionSupport Class to integrate Struts

Here we are going to use Spring's ActionSupport class to integrate Struts.

Action classes of Struts framework do not get started as Spring framework beans, thus cannot be wired through Dependency Injection

If the Action class wants to access beans of Spring framework, it has to access them manually.

Instead of extending Action class, you can extend Spring's org.springframework.web.struts.ActionSupport Class which provides getWebApplicationContext() method to obtain a Spring context

Action Support provides a reference to the current Spring application context, e.g. for bean lookup or resource loading
- Auto-detects a ContextLoaderPlugIn context, falling back to the root WebApplicationContext.
- For typical usage, i.e. accessing middle tier beans, use a root WebApplicationContext.

Below is how your Struts Action Class look like :

public class FindEmployeeAction extends ActionSupport{ //1
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

DynaActionForm searchForm = (DynaActionForm) form;
Long id = Long.parseLong(searchForm.get("id").toString());

System.out.println("Inside FindEmployeeAction id = "+id);

ApplicationContext ctx = getWebApplicationContext(); //2
EmployeeI employeeService = (EmployeeI) ctx.getBean("employeeService"); //3

Employee employee = employeeService.findEmployeeById(id);

if (null == employee) {
return mapping.findForward("failure") ;

request.setAttribute("emp", employee);
return mapping.findForward("success");

Let see how the above Action Class is different from Traditional Action Class and how can it provide a reference to the current Spring context.

1. Extend ActionSupport(org.springframework.web.struts.ActionSupport) Class instead Action Class.
2. ApplicationContext refernce is obtained through getWebApplicationContext() method which is there in ActionSupport Class.
3. Look up for Spring bean using the context reference you have obtained.

Have a look at Spring-config file also.


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


Merits :
1. Very Simple and easy to understand.

1. It couples the Struts action to the Spring framework. If you ever decide to replace Spring, you would have to rewrite the code.
2. As the Struts action isn't under Spring's control, it can't reap the benefits of Spring AOP.
3. Useful when using multiple independent Spring contexts, but for the most part it's not as desirable a solution as the other two choices.

Hope it will be useful...

References :

No comments: