2016年1月31日 星期日

Struts 1.x 整合 Spring 3.x

參考文件

官方說有兩種方式,第一種方式是代理,又分成兩種方法;而第二種方式是繼承,只有一種方法,所以總共有三種方法,以Struts Hello World(Struts一)為例,先可以runStruts1,然後再做整合

要用到的jar檔

pom.xml

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts-core</artifactId>
    <version>1.3.10</version>
</dependency>
    
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>3.2.13.RELEASE</version>
</dependency>
    
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.2.13.RELEASE</version>
</dependency>
    
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-struts</artifactId>
    <version>3.2.13.RELEASE</version>
</dependency>
    
<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
    
<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
</dependency>

※jstl和standard有在Struts Hello World(Struts一)說過,因為太舊了



web.xml

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

※要啟動Spring,一律都要加


applicationContext.xml

<bean name="/xxx" class="controller.LoginAction" />

※先設定好Action路徑




※第一種方法:DelegatingRequestProcessor

struts-config.xml

<form-beans>
    <form-bean name="ooo" type="controller.LoginActionForm">
        <form-property name="username" type="java.lang.String" />
        <form-property name="password" type="java.lang.String" />
    </form-bean>
</form-beans>
    
<action-mappings>
    <action path="/xxx" name="ooo" scope="request" validate="true" input="/index.jsp">
        <forward name="success" path="/s.jsp" />
        <forward name="fail" path="/f.jsp" />
    </action>
</action-mappings>
    
<controller>
    <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor" />
</controller>
    
<message-resources parameter="MessageResources" />
    
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
</plug-in>

※plug-in是為了讓系統知道applicationContext.xml放在哪

※主要是controller,這時action的type就沒用了,所以拿掉了。
當JSP傳來xxx.do時,action的path會收到,這是controller在把它丟到applicationContext.xml,然後找到對應的Action



※第二種方法:DelegatingActionProxy

struts-config.xml

<form-beans>
    <form-bean name="ooo" type="controller.LoginActionForm">
        <form-property name="username" type="java.lang.String" />
        <form-property name="password" type="java.lang.String" />
    </form-bean>
</form-beans>
    
<action-mappings>
    <action path="/xxx" type="org.springframework.web.struts.DelegatingActionProxy" name="ooo" scope="request" validate="true" input="/index.jsp">
        <forward name="success" path="/s.jsp" />
        <forward name="fail" path="/f.jsp" />
    </action>
</action-mappings>
    
<message-resources parameter="MessageResources" />
    
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
</plug-in>

※action的type變成Spring的代理機制了,整個controller都不要了,很方便,是三種方法最好的方式




※第三種方法:ActionSupport

LoginAction.java

public class LoginAction extends ActionSupport {
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        WebApplicationContext ctx = this.getWebApplicationContext();
        LoginAction l = (LoginAction) ctx.getBean("/xxx");
    
        LoginActionForm laf = (LoginActionForm) form;
    
        if (laf.getUsername().trim().equals("aaa") && laf.getPassword().trim().equals("111")) {
            return mapping.findForward("success");
        } else {
            return mapping.findForward("fail");
        }
    }
}

※本來是繼承org.apache.struts.action.Action,要改繼承org.springframework.web.struts.ActionSupport,不是Struts2的ActionSupport哦!

※然後就通過WebApplicationContext再getBean就能取的applicationContext.xml的實體了,看是要取得Dao對資料庫做事或什麼的,這種方法是最破的,因為產生了耦合



※取得applicationContext.xml

web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <!-- <param-value>classpath*:applicationContext.xml</param-value> -->
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

※前面三種方法都是使用plug-in取得,其實寫在web.xml也可以,兩者擇一,但註解那行不知為什麼會抓不到,以繼承ActionSupport為例,run的時候都很順,但一執行到getBean時就會報找不到xxx的錯,也就是找不到applicationContext.xml

沒有留言:

張貼留言