Thursday, 21 April 2016

Integrating Spring with Struts 2



To integrate Struts 2 and Spring, get and include the "struts2-spring-plugin-xxx.jar" library into your project classpath.

Dependency libraries :
  • Struts 2 = struts2-core 2.1.8
  • Spring framework = spring 2.5.6, spring-web 2.5.6
  • Struts 2 + Spring plugins = struts2-spring-plugin 2.1.8

Spring Listener

Configure the Spring listener "org.springframework.web.context.ContextLoaderListener" in web.xml file.

web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Struts 2 Web Application</display-name>
<filter>
  <filter-name>struts2</filter-name>
  <filter-class> 
 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 
  </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>
</web-app>


Spring Bean

Register all the Spring’s Beans in the applicationContext.xml file, the Spring listener will locate this xml file automatically.
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="userBo" class="com.genius.user.bo.impl.UserBoImpl" />
<bean id="userSpringAction
      class="com.genius.user.action.UserSpringAction">
  <property name="userBo" ref="userBo" />
</bean>
</beans>

UserBo.java
package com.genius.user.bo;
public interface UserBo {
  public void printUser();
}

UserBoImpl.java
package com.genius.user.bo.impl;
import com.genius.user.bo.UserBo;
public class UserBoImpl implements UserBo{
  public void printUser(){
    System.out.println("printUser() is executed...");
  }
}


Struts Action and configuration

UserSpringAction.java
package com.genius.user.action;
import com.genius.user.bo.UserBo;
public class UserSpringAction{
  // DI via Spring
  UserBo userBo;
  public UserBo getUserBo() {
    return userBo;
  }
  public void setUserBo(UserBo userBo) {
    this.userBo = userBo;
  }
  public String execute() throws Exception {
   userBo.printUser();
   return "success";
  }
}

Struts.xml
Declared all the relationship here.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 <constant name="struts.devMode" value="true" />
 <package name="default" namespace="/" extends="struts-default">
   <action name="userAction
           class="com.genius.user.action.UserAction" >
     <result name="success">pages/user.jsp</result>
   </action>
   <action name="userSpringAction" class="userSpringAction" >
     <result name="success">pages/user.jsp</result>
   </action>
 </package>
</struts>


Demo

Now, all the Struts 2 and Spring integration work is done, now see the following use case to access the Spring’s “userBo” bean.

Case 1 : Make Spring act as the Struts 2 Action class, and access the Spring’s bean.
In this example, the userSpringAction is act as the Struts 2 Action class, and you can DI the Spring’s userBo bean with normal Spring’s way.
struts.xml
<action name="userSpringAction" class="userSpringAction" >
   <result name="success">pages/user.jsp</result>
</action>

applicationContext.xml
<bean id="userSpringAction
      class="com.genius.user.action.UserSpringAction">
   <property name="userBo" ref="userBo" />
</bean>
To access this action, use the URL : http://localhost: 8080/Struts2Example/userSpringAction

Case 2 : Access the Spring’s bean in Struts 2 Action class.
By default, Spring listener enables “autowiring by matching the bean name“. So, it will pass the Spring’s “userBo” bean into the UserAction via setUserBo() automatically. See below Struts 2 Action : 
The Spring’s autowiring feature can change to name(default), type, auto or constructor, you may need to consult this Struts 2 Spring plugin documentation.

UserAction.java
package com.genius.user.action;
import com.genius.user.bo.UserBo;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
  // DI via Spring
  UserBo userBo;
  public UserBo getUserBo() {
    return userBo;
  }
  public void setUserBo(UserBo userBo) {
    this.userBo = userBo;
  }
  public String execute() throws Exception {
    userBo.printUser();
    return SUCCESS;
  }
}
To access this action, use the URL : http://localhost:8080/Struts2Example/userAction


WebApplicationContextUtils

Alternatively, you can use the Spring’s generic WebApplicationContextUtils class to get the Spring’s bean directly.
package com.genius.user.action;
import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.genius.user.bo.UserBo;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{
  public String execute() throws Exception {
    WebApplicationContext context = 
      WebApplicationContextUtils.getRequiredWebApplicationContext 
      (ServletActionContext.getServletContext());
    UserBo userBo1 = (UserBo) context.getBean("userBo");
    userBo1.printUser();
    return SUCCESS;
  }
}


Reference


No comments:

Post a Comment

Note: only a member of this blog may post a comment.