strut2提供了一種非常簡單的方式來實現與spring的整合,記得以前用struts1還要改配置文件,struts通過一種“可插撥式”的插件,實現了與Spring框架的整合。在實現應用中,只需要把struts2-spring-plugin-x.x.x.x.jar(其中的xxxx為版本號)文件拷到應用的lib下即可。

       Struts2提供了兩種基本的整合策略:

1.         Action實例交給Spring容器來負責生成,管理,通過這種方式,可以充分利用Spring容器的IOC特性,提供最好的解耦;

2.         利用Spring插件的自動裝配方式,當Spring插件創建Action實例之后,立即將Spring窗口中對應的業務邏輯組件注入Action實例。

下面看看兩個實例:

 首先:把spring.jar commons-logging.jarstruts2-spring-plugin-x.x.x.x.jar等相關的包拷到lib下,修改web.xml:添加linstener

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

這樣配置之后,Spring自動查找WEB-INF路徑下的applicationContext.xml配置文件。當然也可以自己指定配置文件,則需要在web.xml<context-param>元素,如下:

<context-param>

       <param-name>contextConfigLocation</param-name>

       <param-value>/WEB-INF/my.xml</param-value>

</context-param>

 

1.用第一方式實現struts2spring的整合

           (1)一個簡單的用戶登錄頁面login.jsp:
         
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>用戶登錄</title>
</head>
<body>
<s:form action="Login">
    
<s:textfield name="username" label="用戶信息"/>
<s:textfield name="password" label="用戶信息"/>
    
<s:submit value="登錄"/>
</s:form>
</body>
</html>
      (2)實現控制器邏輯(LoginAction.java):
package my;
import org.apache.log4j.Logger;
import com.opensymphony.xwork2.Action;
public class LoginAction implements Action
{
    
private static final Logger log = Logger.getLogger(LoginAction.class);
    
private MyService ms;
    
private String tip;
    
private String username;
    
private String password;
    
public void setMs(MyService ms)
    
{
        
this.ms = ms;
    }

    
public void setPassword(String password)
    
{
        
this.password = password;
    }

    
public String getPassword()
    
{
        
return this.password;
    }

    
public void setUsername(String username)
    
{
        
this.username = username;
    }

    
public String getUsername()
    
{
        
return this.username;
    }


    
public void setTip(String tip)
    
{
        
this.tip=tip;
    }

    
public String getTip()
    
{
        
return this.tip;
    }

    
public String execute()throws Exception
    
{    
            
if (ms.valid(getUsername(),getPassword()))
            
{
                setTip(
"呵,整合成功!");
                
return SUCCESS;
            }

            
else
                
return ERROR;
        
    }

}
(3)struts2的配置文件(struts.xml):注意class屬性不是指向一個action,而是指向spring 的一個bean
<?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>
    
<package name="strutsqs" extends="struts-default">
        
<action name="Login" class="loginAction">
            
<result name="error">/login.jsp</result>
            
<result name="success">/success.jsp</result>
        
</action>
    
</package>
</struts>

(4)業務邏輯接口(MyService.java):
package my;
public interface MyService
{
    
public boolean valid(String username , String password);
}
(5)實現業務邏輯(MySeviceImpl.java):
package my;
public class MyServiceImpl implements MyService
{
    
public boolean valid(String username , String password)
    
{
        
if (username.equals("hello"&& password.equals("world"))
        
{
            
return true;
        }

        
else
            
return false;
    }

}
(6)Spring的配置文件my.xml:
<?xml version="1.0" encoding="GBK"?>
<!-- 指定Spring配置文件的Schema信息 -->
<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.xsd">
    
<bean id="myservice" class="my.MyServiceImpl"/>
    
<bean id="loginAction" class= "my.LoginAction" scope="prototype">
        
<property name="ms" ref="myservice"/>
    
</bean>
</beans>
ok.整合完畢。
2.使用自動裝配方式整合
    (1)指定自動裝配的策略,讓Spring 自動管理Bean與Bean之間的依賴有關系,無需使用ref顯式指定依賴Bean ,Spring容器會自動檢查XML配置文件內容,為主調Bean 注入依賴Bean。Spring 提供了3種裝配策略,通過修改struts.objectFactory.spring.autoWire常量指定,可接受的3個值:
     1.name:根據屬性名自動裝配,這個是默認值。
     2.type根據屬性類型自動裝配
     3.auto自動檢測需要使用哪種自動裝配方式
  在上面的簡單應用中,只需要修改配置文件即可:
    1.修改struts.xml配置文件:class屬性與沒有整合時一樣的。。
<?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>
    
    
<package name="strutsqs" extends="struts-default">

        
<action name="Login" class="my.LoginAction">

            
<result name="error">/login.jsp</result>
            
<result name="success">/success.jsp</result>
        
        
</action>
        
    
</package>

</struts>
2.修改spring的配置文件my.xml,因為action中需要業務邏輯組件名為ms,所以必須在配置業務邏輯組件時,指定其id屬性為ms
 
<?xml version="1.0" encoding="GBK"?>
<!-- 指定Spring配置文件的Schema信息 -->
<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.xsd">
    
<bean id="ms" class="my.MyServiceImpl"/>

</beans>
ok,完成。。