DispatchAction
=======================
org.apache.struts.actions.DispatchAction.java
DispatchAction的作用簡單地說就是把原來我們寫在多個acton里的操作放在同一個 action里處理。
舉個例子就是如果在你的系統中有文章的管理操作,那么通常有以下操作:添加文章、察看文章、搜索文章等等,這樣的話一般你會寫 三個action[ArtilceSaveAction ArticleViewAction ArticleSearchAction ]分別處理各個操作,雖然說這樣看起來是非常清晰、流暢的操作,但是你會發現在三個action理由太多的相同的東西。現在利用DispatchAction,我們可以把“相似”的action放在一個action里操作。
下面以上邊的三個action和到一個action里為例:
import ****;
import org.apache.struts.actions.DispatchAction;
public class ArticleAction extends DispatchAction{
/**
*AritcleAddAction
*/
public ActionForward add(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
...
...
}
/**
*AritcleViewAction
*/
public ActionForward view(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
...
...
}
/**
*AritcleSearchAction
*/
public ActionForward search(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
...
}
}
好了,該action的框架已經完成,但是要想可用,還要有一步不可少的操作,
那就是更改你的action mapping ,還以上邊的例子,如下:
<action path="/article"
input="/article/***.jsp"
parameter="method" <!--#####################-->
scope="request"
type="com.***.ArticleAction"
validate="false">
<forward name="Success" path="/article/***.jsp" redirect="true"/>
</action>
看到上邊你會發現,它和我們通常的寫法多個一項:“parameter="method"”,這是有道理的并且非常重要:DispatchAction會根據具體的method值來確定調用add,view 或者search ,如下面的來自client的請求,article.do?method=add 則會觸發添加文章的操作。
LookupDispatchAction
========================
org.apache.struts.actions.LookupDispatchAction.java
從名字大概我們也能看出LookupDispatchAction是DispatchAction的子類。他們從功能上有許多相似的地方。 通常它主要應用于“在一個表單中有多個提交按鈕而這些按鈕又有一個共同的名字”,這些按鈕的名字要和具體的action mapping中的parameter的值對應。[這點很重要]
如下代碼截取自struts-config.xml:
<action path="/editArticle"
type="com.****.EditArticleAction"
name="AtricleForm"
scope="request"
parameter="action"><!--按鈕的名字此處為“action”-->
<forward name="success" path="/***.jsp"/>
</action>
下面給出一個jsp頁面的表單部分
<html:form action="/editArticle"/>
<html:submit property="action">
<bean:message key="button.view"/>
</html:submit>
<html:submit property="action">
<bean:message key="button.delete"/>
</html:submit>
</html:form>
那么相應的ApplicationResources.properties中就會有如下片斷:
button.view=View The Article
button.delete=Delete The Atricle
此時還并為完成,在LookupDispatchAction中有一個抽象方法:
/**
* Provides the mapping from resource key to method name
*
*@return Resource key / method name map
*/
protected abstract Map getKeyMethodMap();
這個方法你應該在EditArticleAction中實現,如下:
protected Map getKeyMethodMap(){
Map map = new HashMap();
map.put("button.view", "view");
map.put("button.delete", "delete");
return map;
}
好了,假設在你的EditArticleAction有如下方法:
public ActionForward view(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
//......
//......
return mapping.findForward("success");
}
public ActionForward delete(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
//......
//......
return mapping.findForward("success");
}
下面實例幾個假設client端的請求:
http://....../editArticle.do此時頁面有兩個按鈕,按鈕1“View The Article”,"",按鈕2“Delete The Atricle”
當提交按鈕1時調用EditArticleAction里的view方法;
當提交按鈕2時調用EditArticleAction里的delete方法;
以下還有一點說明;
如果我有一個按鈕要觸發action的AA方法,但是在該action沒有AA方法,此時將拋出異常;如果該action中有兩個AA方法,則會調用第一個。
SwitchAction
========================
org.apache.struts.actions.SwitchAction
SwitchAction主要在多模塊應用中執行模塊切換作用。
在Struts多模塊工程中有兩種方法可以切換到新模塊上.首先,您可以在 struts-config.xml 中創建一個新的全局或局部轉發“success”(參見下面代碼)。然后可以使用 mapping.findForward("success") 切換到這個新模塊上來。
<forward name="success" contextRelative="true"
path="/newModule/index.do" redirect="true"/>
contextRelative=true的意思是path是相對于應用上下文的路徑,contextRelative=false則是指path是相對于模塊上下文的路徑。
其次,創建一個如下所示的操作,其類型是一個 SwitchAction 操作:
<action path="/switchTo" type="org.apache.struts.actions.SwitchAction" validate="false" />
然后在 struts-config-newModule.xml 中創建如下所示的操作映射。
<action-mappings>
<action path="/index" type="com.asprise.struts.newmodule.action.IndexAction">
<forward name="success" path="/index.jsp"/>
</action>
</action-mappings>
接下來,編寫 easyStruts/newModule/index.jsp 的代碼。index.jsp 只會顯示一條消息“<h1>Youare in module: newModule</h1>”。現在,啟動 Tomcat 服務器,并輸入 http://127.0.0.1:8080/easyStruts/switchTo.do?prefix=/newModule&page=/index.do既可。如果您想切換回默認的模塊,只需輸入 http://127.0.0.1:8080/easyStruts/switchTo.do?prefix=&page=/owner.jsp 即可。