Struts 2.0 and Spring

In this post, I will describe how to do the same using Struts 2.0. The only major step that needs to be done here is to override the default Struts 2.0 OjbectFactory. Changing the ObjectFactory to Spring give control to Spring framework to instantiate action instances etc. Most of the code is from the previous post, but I will list only the additional changes here.

also read:

1)Changing the default Object factory: In order to change the Ojbect factory to Spring, you have to add a declaration in the file.

struts.objectFactory = spring
struts.devMode = true

2)The Action class: Here is the code for the action class

package actions;

import java.util.List;

import business.BusinessInterface;

import com.opensymphony.xwork2.ActionSupport;

public class SearchAction extends ActionSupport {
private BusinessInterface businessInterface;

private String minSalary;

private String submit;

private List data;

public String getSubmit() {
 return submit;

public void setSubmit(String submit) {
 this.submit = submit;

public BusinessInterface getBusinessInterface() {
 return businessInterface;

  public String execute() throws Exception {
 try {
   long minSal = Long.parseLong(getMinSalary());
   System.out.println("Business Interface: " + businessInterface + "Minimum salary : " + minSal);
   data = businessInterface.getData(minSal);
   System.out.println("Data : " + data);

 } catch (Exception e) {

 return SUCCESS;

public void setBusinessInterface(BusinessInterface bi) {
 businessInterface = bi;

public String getMinSalary() {
 return minSalary;

public void setMinSalary(String minSalary) {
 this.minSalary = minSalary;

public List getData() {
 return data;

public void setData(List data) { = data;
  • The Action class here does not have access to the HttpServetRequest and HttpServletResponse. Hence the action class itself was changed to the session scope for this example (see below)
  • In order for the action class to be aware of the Http Session, the action class has to implement the ServletRequestAware interface, and define a setServletRequest method, which will be used to inject the ServletRequest into the action class.
  • The BusinessInterface property is injected by Spring framework.

3)The struts Configuration:

     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
<package name="Struts2Spring" namespace="/actions" extends="struts-default">
 <action name="search" class="actions.SearchAction">

The action’s class attribute has to map the id attribute of the bean defined in the spring bean factory definition.

4)The Spring bean factory definition:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" xmlns:xsi=""
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
 <property name="driverClassName">
 <property name="url">
 <property name="username">
 <property name="password">

<!-- Configure DAO -->
<bean id="empDao" class="data.DAO">
 <property name="dataSource">
   <ref bean="dataSource"></ref>
<!-- Configure Business Service -->
<bean id="businessInterface" class="business.BusinessInterface">
 <property name="dao">
   <ref bean="empDao"></ref>
<bean id="actions.SearchAction" name="search" class="actions.SearchAction" scope="session">
    <property name="businessInterface" ref="businessInterface" />
  • The bean definition for the action class contains the id attribute which matches the class attribute of the action in struts.xml
  • Spring 2’s bean scope feature can be used to scope an Action instance to the session, application, or a custom scope, providing advanced customization above the default per-request scoping.

5)The web deployment descriptor:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="" xmlns:xsi=""





The only significant addition here is that of the RequestContextListener. This listener allows Spring framework, access to the HTTP session information.

6)The JSP file: The JSP file is shown below. The only change here is that the action class, instead of the Data list is accessed from the session.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="" prefix="display"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ page import="actions.SearchAction,beans.Employee,business.Sorter,java.util.List,org.displaytag.tags.TableTagParameters,org.displaytag.util.ParamEncoder"%>
<title>Search page</title>
<link rel="stylesheet" type="text/css" href="/StrutsPaging/css/screen.css" />
<body bgcolor="white">
<s:form action="/actions/search.action">
    <td>Minimum Salary:</td>
    <td><s:textfield label="minSalary" name="minSalary" /></td>
    <td colspan="2"><s:submit name="submit" /></td>

 SearchAction action = (SearchAction)session.getAttribute("actions.SearchAction");
 session.setAttribute("empList", action.getData());
  if (session.getAttribute("empList") != null) {
   String sortBy = request.getParameter((new ParamEncoder("empTable")).encodeParameterName(TableTagParameters.PARAMETER_SORT));
   Sorter.sort((List) session.getAttribute("empList"), sortBy);


<display:table name="sessionScope.empList" pagesize="4" id="empTable" sort="external" defaultsort="1" defaultorder="ascending" requestURI="">
<display:column property="empId" title="ID" sortable="true" sortName="empId" headerClass="sortable" />
<display:column property="empName" title="Name" sortName="empName" sortable="true" headerClass="sortable" />
<display:column property="empJob" title="Job" sortable="true" sortName="empJob" headerClass="sortable" />
<display:column property="empSal" title="Salary" sortable="true" headerClass="sortable" sortName="empSal" />


also read:

7)The Other required classes: The following other classes have been used for the example, and they can be obtained from the previous posts (1, 2).


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pin It on Pinterest

Share This

Share this post with your friends!

Share This

Share this post with your friends!