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

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

1.         Action實(shí)例交給Spring容器來(lái)負(fù)責(zé)生成,管理,通過(guò)這種方式,可以充分利用Spring容器的IOC特性,提供最好的解耦;

2.         利用Spring插件的自動(dòng)裝配方式,當(dāng)Spring插件創(chuàng)建Action實(shí)例之后,立即將Spring窗口中對(duì)應(yīng)的業(yè)務(wù)邏輯組件注入Action實(shí)例。

下面看看兩個(gè)實(shí)例:

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

<listener>

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

</listener>

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

<context-param>

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

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

</context-param>

 

1.用第一方式實(shí)現(xiàn)struts2spring的整合

           (1)一個(gè)簡(jiǎn)單的用戶登錄頁(yè)面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)實(shí)現(xiàn)控制器邏輯(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屬性不是指向一個(gè)action,而是指向spring 的一個(gè)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)業(yè)務(wù)邏輯接口(MyService.java):
package my;
public interface MyService
{
    
public boolean valid(String username , String password);
}
(5)實(shí)現(xiàn)業(yè)務(wù)邏輯(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.使用自動(dòng)裝配方式整合
    (1)指定自動(dòng)裝配的策略,讓Spring 自動(dòng)管理Bean與Bean之間的依賴有關(guān)系,無(wú)需使用ref顯式指定依賴Bean ,Spring容器會(huì)自動(dòng)檢查XML配置文件內(nèi)容,為主調(diào)Bean 注入依賴Bean。Spring 提供了3種裝配策略,通過(guò)修改struts.objectFactory.spring.autoWire常量指定,可接受的3個(gè)值:
     1.name:根據(jù)屬性名自動(dòng)裝配,這個(gè)是默認(rèn)值。
     2.type根據(jù)屬性類型自動(dòng)裝配
     3.auto自動(dòng)檢測(cè)需要使用哪種自動(dòng)裝配方式
  在上面的簡(jiǎn)單應(yīng)用中,只需要修改配置文件即可:
    1.修改struts.xml配置文件:class屬性與沒(méi)有整合時(shí)一樣的。。
<?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,因?yàn)閍ction中需要業(yè)務(wù)邏輯組件名為ms,所以必須在配置業(yè)務(wù)邏輯組件時(shí),指定其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,完成。。