本文為原創(chuàng),如需轉(zhuǎn)載,請注明作者和出處,謝謝!
上一篇:Struts1.x系列教程(17):使用IncludeAction和ForwardAction類包含和轉(zhuǎn)入Web資源
在使用Struts動(dòng)作時(shí),每一個(gè)Action都需要編寫一個(gè)類,并且要在struts-config.xml進(jìn)行配置。這對于一個(gè)擁有很多Action的Web程序來說,工作量是非常大的。為此,Struts提供了DispatchAction類,這個(gè)類允許將一個(gè)Action作為一個(gè)方法來調(diào)用。在Web瀏覽器中通過請求參數(shù)來指定要調(diào)用的動(dòng)作。
雖然DispatchAction類是一個(gè)抽象類,但其中卻沒有一個(gè)抽象方法。因此,DisplatchAction的子類不用實(shí)現(xiàn)任何DisplatchAction類中的方法。但如果要處理Action代碼,就必須根據(jù)相應(yīng)的Action來編寫Action方法。一個(gè)Action方法除了方法名和execute方法不一樣外,其他的都和execute方法完全一樣。但編寫Action方法時(shí)要注意,Action方法名必須和用于指定動(dòng)作的請求參數(shù)值一致(大小寫也必須一致)。在下面的例子中演示了通過DispatchAction類實(shí)現(xiàn)方法和Action的對應(yīng)。在這個(gè)例子中,有三個(gè)方法:fr、en和unspecificed。其中fr和en是兩個(gè)Action方法,分別用于將當(dāng)前頁面轉(zhuǎn)發(fā)到法文和英文頁面,而當(dāng)用于指定Action的請求參數(shù)不存在時(shí),則調(diào)用unspecificed方法(在這個(gè)方法中將當(dāng)前頁面轉(zhuǎn)發(fā)到中文頁面)。在這個(gè)程序中使用的用于指定Action的請求參數(shù)為language(讀者可以指定自己的請求參數(shù))。
在<samples工程目錄>\src\action目錄建立一個(gè)MyDispatchAction.java文件,代碼如下:
package action;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.actions.*;
public class MyDispatchAction extends DispatchAction
{
// forward到法文頁面
public ActionForward fr(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
try
{
RequestDispatcher rd = request.getRequestDispatcher("/newGlobal.jsp?language=fr");
rd.forward(request, response);
}
catch (Exception e)
{
}
return null;
}
// forward到英文頁面
public ActionForward en(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
try
{
RequestDispatcher rd = request.getRequestDispatcher("/newGlobal.jsp?language=en");
rd.forward(request, response);
}
catch (Exception e)
{
}
return null;
}
// 在未使用language=fr和language=en作為訪問參數(shù)的情況時(shí)調(diào)用這個(gè)方法
protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
try
{
// forward到中文頁面
RequestDispatcher rd = request.getRequestDispatcher("/newGlobal.jsp?language=zh");
rd.forward(request, response);
}
catch (Exception e)
{
}
return null;
}
}
在struts-config.xml文件中加入如下的配置代碼來配置MyDispatchAction:
<action path="/locale" type="action.MyDispatchAction" parameter="language"/>
其中parameter參數(shù)表示用于指定Action的請求參數(shù)名。
在啟動(dòng)Tomcat后,可通過如下的URL進(jìn)行測試:
顯示英文頁面:
http://localhost:8080/samples/locale.do?language=en
顯示法文頁面:
http://localhost:8080/samples/locale.do?language=fr
顯示中文頁面(默認(rèn)頁面):
http://localhost:8080/samples/locale.do
雖然上面的代碼可以很好地調(diào)用相應(yīng)的Action方法,但在一些情況時(shí),如請求參數(shù)language指定的Action方法不存在時(shí),就會(huì)拋出異常。那么如果我們想在非正常情況下都調(diào)用默認(rèn)的處理Action動(dòng)作的方法(也就是unspecificed方法)該怎么辦呢?
實(shí)現(xiàn)上,實(shí)現(xiàn)這個(gè)功能也非常簡單,只要我們知道在什么條件下調(diào)用unspecified方法,然后在非正常情況下,都將條件設(shè)為調(diào)用unspecified方法的條件就可實(shí)現(xiàn)這個(gè)功能。在查看DispatchAction類的源代碼后,可找到如下的代碼片段:
if (name == null) // name表示Action方法名
{
return this.unspecified(mapping, form, request, response);
}
從上面的代碼可知,只有當(dāng)name為null時(shí)才會(huì)調(diào)用unspecified方法。這個(gè)name值實(shí)際上也就是language參數(shù)的值。也就是說,只有當(dāng)language參數(shù)不存在時(shí),name才會(huì)為null。如果在language的參數(shù)值所指的Action方法不存在時(shí)或者name為空串的情況下都將name設(shè)為null,那么就可以達(dá)到我們的目的。
在DispatchAction類中有一個(gè)dispatchMethod方法,可以在這個(gè)方法中處理請求參數(shù)值為空串(也就是當(dāng)“language=”時(shí)將方法名設(shè)為null)和Action方法未找到的情況。在Action類中有兩個(gè)特殊方法:execute和perform。如果調(diào)用了這兩個(gè)方法,將會(huì)出現(xiàn)遞歸調(diào)用的情況。因此,在調(diào)用這兩個(gè)方法時(shí)也需要將方法名設(shè)為null。這個(gè)工作可以在DispatchAction類的getMethodName方法中實(shí)現(xiàn)。為了完成這個(gè)功能,需要將上面的代碼放到MyDispatchAction類中。
protected ActionForward dispatchMethod(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response, String name) throws Exception
{
ActionForward af = null;
// 在language參數(shù)值為空串的情況下,將方法名賦為null
if (name != null) // name表示Action方法名,也是language的參數(shù)值
if (name.equals(""))
name = null;
try
{
af = super.dispatchMethod(mapping, form, request, response, name);
}
catch(NoSuchMethodException e) // 處理Action方法未找到的情況
{
// 在language的參數(shù)值沒有對應(yīng)的Action方法時(shí),將方法名賦為null
name = null;
af = super.dispatchMethod(mapping, form, request, response, name);
}
return af;
}
// 當(dāng)language的參數(shù)值為execute或perfore時(shí),必須將方法名賦為null,否則會(huì)出現(xiàn)遞歸調(diào)用
protected String getMethodName(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response,
String parameter) throws Exception
{
String methodName = super.getMethodName(mapping, form, request, response, parameter);
if ("execute".equals(methodName) || "perform".equals(methodName))
return null; // 如果訪問的是execute和perform,直接將方法名設(shè)為null
return methodName;
}
現(xiàn)在我們可以用任何請求參數(shù)來訪問locale動(dòng)作,只要未找到Action方法,就會(huì)調(diào)用默認(rèn)的unspecified方法。讀者可以使用如下的URL來實(shí)驗(yàn)一下:
http://localhost:8080/samples/locale.do?language=
http://localhost:8080/samples/locale.do?language=unknown
下一篇:Struts1.x系列教程(19):LookupDispatchAction類處理一個(gè)form多個(gè)submit
新浪微博:http://t.sina.com.cn/androidguy 昵稱:李寧_Lining