Integrate with struts2 and spring

 This post will describe how to do integrate with struts2 and spring. The only major step that needs to be done here is to override the default Struts 2.0 ObjectFactory. 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.

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

struts.objectFactory = spring

struts.devMode = true

struts.enable.DynamicMethodInvocation = false

src/struts.properties

  1. 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) {

                  e.printStackTrace();

            }

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

            this.data = data;

      }

}

SearchAction.java

  1.  
    • 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.
  2. The struts Configuration:

 

 

<!DOCTYPE struts PUBLIC      “-//Apache Software Foundation//DTD Struts Configuration 2.0//EN”      http://struts.apache.org/dtds/struts-2.0.dtd&#8221;>

<struts>

      <package name=“Struts2Spring” namespace=“/actions”

            extends=“struts-default”>

            <action name=“search” class=“actions.SearchAction”>

                  <result>/search.jsp</result>

            </action>

      </package>

</struts>

src/struts.xml

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

The Spring bean factory definition

 

<?xml version=“1.0” encoding=“UTF-8”?>

<beans xmlns=http://www.springframework.org/schema/beans&#8221;

      xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance&#8221;

      xsi:schemaLocation=http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd&#8221;

      default-autowire=“autodetect”>

      <bean id=“dataSource”

            class=“org.apache.commons.dbcp.BasicDataSource”

            destroy-method=“close”>

            <property name=“driverClassName”>

                  <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>

            </property>

            <property name=“url”>

                  <value>

                        jdbc:microsoft:sqlserver://WEBSERVER:1433;DatabaseName=EBOOK_TEST

                  </value>

            </property>

            <property name=“username”>

                  <value>sa</value>

            </property>

            <property name=“password”>

                  <value>sa</value>

            </property>

      </bean>

      <!– Configure DAO –>

      <bean id=“empDao” class=“data.DAO”>

            <property name=“dataSource”>

                  <ref bean=“dataSource”></ref>

            </property>

      </bean>

      <!– Configure Business Service –>

      <bean id=“businessInterface” class=“business.BusinessInterface”>

            <property name=“dao”>

                  <ref bean=“empDao”></ref>

            </property>

      </bean>

      <bean id=“actions.SearchAction” name=“search”

            class=“actions.SearchAction” scope=“session”>

            <property name=“businessInterface” ref=“businessInterface”/>

      </bean>

</beans>

 

 

WEB-INF/applicationContext.xml

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

 

  1. The web deployment descriptor

<?xml version=“1.0” encoding=“UTF-8”?>

<web-app id=“WebApp_9” version=“2.4”

      xmlns=http://java.sun.com/xml/ns/j2ee&#8221;

      xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance&#8221;

      xsi:schemaLocation=http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&#8221;>

      <display-name>Struts2Spring</display-name>

      <filter>

            <filter-name>struts2</filter-name>

            <filter-class>

                  org.apache.struts2.dispatcher.FilterDispatcher

            </filter-class>

      </filter>

      <filter-mapping>

            <filter-name>struts2</filter-name>

            <url-pattern>/*</url-pattern>

      </filter-mapping>

      <listener>

            <listener-class>

                  org.springframework.web.context.ContextLoaderListener

            </listener-class>

      </listener>

      <listener>

            <listener-class>

                  org.springframework.web.context.request.RequestContextListener

            </listener-class>

      </listener>

      <welcome-file-list>

            <welcome-file>index.html</welcome-file>

      </welcome-file-list>

</web-app>

 

web.xml

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

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s