使用Action類別

来源:互联网 发布:linux服务器教程 编辑:程序博客网 时间:2024/06/17 13:56
 
 在Struts中,ActionServlet擔任分配工作的控制器角色,實際上的工作是交給 Action物件來進行,ActionServlet由ActionMapping得知所使用的Action物件,將工作交給它,並在最後由Action 物件得到一個ActionForward物件,ActionServlet使用這個ActionForward來知道下一個forward的對象。
 對於Struts,一個ActionMapping只會生成一個Action物件,當請求到達時,會檢查所需的Action物件是否存在,如果不存在則生成一個,之後一直使用它,由於Action物件會一直存在,所以使用Action物件必須注意到執行緒安全問題。
 我們透過繼承Action類別來使用它,在Struts 1.1中,我們會重新定義execute()方法,在Struts 1.0中的perform()方法已經不建議使用;execute()方法有兩個接收不同參數的版本:
public ActionForward execute(ActionMapping mapping,                     ActionForm form,                     ServletRequest request,                     ServletResponse response)throws Exception;public ActionForward execute(ActionMapping mapping,                     ActionForm form,                     HttpServletRequest request,                     HttpServletResponse response)throws Exception;

 由於Action通常處理HTTP相關請求,所以我們通常會使用第二個,execute()方法最後要傳回ActionForward物件給ActionServlet。
 在MVC/Model 2中,理想上所有的請求都經由ActionServlet來協調轉發,客戶端不會直接指定真正的位址來請求資源,我們下面直接來看個例子,使用Action物件處理這個功能,首先撰寫一個Action類別:
GetAction.java
package onlyfun.caterpillar;                                                                               import javax.servlet.http.*;import org.apache.struts.action.*;                                                                               public class GetAction extends Action {    public ActionForward execute(ActionMapping mapping,                                 ActionForm form,                                 HttpServletRequest request,                                 HttpServletResponse response)    throws Exception {        String resource = request.getParameter("resource");        if(resource != null && resource.length() != 0) {            request.setAttribute("resource", resource);            return mapping.findForward("resource");            // return new ActionForward("resource");        }                                                                                       return mapping.findForward("welcome");    }}

 在這個類別中,如果客戶端的請求中包括了請求參數resource,則直接返回一個ActionForward,對象為請求參數的內容,原本我們應該是return new ActionForward("resource"),不過為了說明方便,我們將查找struts-config.xml中的forward對象,如果客戶端沒有包括resource對象,則直接查找"welcome"的forward對象。
 這個類別編譯過後,應該位於WEB-INF下並包括package階層,接著我們要在struts-config.xml中加以定義:
struts-config.xml
<struts-config>    <global-forwards>        <forward            name="welcome"            path="/Welcome.do"/>    </global-forwards>                                                                                   <action-mappings>        <action            path="/Welcome"            type="org.apache.struts.actions.ForwardAction"            parameter="/pages/Welcome.jsp"/>                                                                                       <action            path="/GetAction"            type="onlyfun.caterpillar.GetAction">            <forward                name="resource"                path="/pages/showres.jsp"/>        </action>    </action-mappings>                                                                                   <message-resources parameter="resources.application"/></struts-config>

 在<action-mappings>定義中,如果是/GetAction開頭的URI請求,都會交給GetAction物件來處理,其中<forward>標籤定義了如果查找"resource"的forward對象,實際該forward的路徑, showres.jsp的內容如下,只是簡單的顯示請求資源的路徑而已:
showres.jsp
<%@ taglib uri="/tags/struts-bean" prefix="bean" %><%@ taglib uri="/tags/struts-html" prefix="html" %><%@page contentType="text/html; charset=Big5"%><html:html locale="true"><head><title><bean:message key="welcome.title"/></title><html:base/></head><body bgcolor="white">                                                                               <H1>請求資源 ${ resource }</H1>                                                                               </body></html:html>

 啟動Servlet容器,並使用http://localhost:8080/HelloStruts/GetAction.do?resource=/images/struts.jpg來瀏覽,您會得到以下的內容:
<html lang="zh"><head><title>哈囉!Struts!</title><base href="http://localhost:8080/HelloStruts/pages/showres.jsp"></head><body bgcolor="white"><H1>請求資源 /images/struts.jpg</H1></body></html>

 Action物件是執行工作的物件,它主要的工作通常有:
*驗證使用者的進程狀態
*進一步驗證表單物件的資訊
*更新應用程式中物件的狀態
*處理客戶端的請求
*返回ActionForward給ActionServlet
 為了要能夠重用Action物件,通常建議不要在Action中加入過多的邏輯,普遍認為Action物件也應屬於控制器角色,除了以上必要的工作之外,建議將與業務相關的工作交給輔助類別,減少Action物件中的邏輯,以使得Action物件容易理解它的職責。
 基本上不只有Action物件應保持職責清晰,就Struts而言,它的工作是輔助建議MVC/Model 2架構,每個相關的組件除了這個目的之外,沒有其它的目的,Struts建立起來的架構也鼓勵重用,應當將業務相關的邏輯交由其它的物件來處理,而不是交給Struts組件。
原创粉丝点击