歡迎您光臨本站 註冊首頁

在使用Struts動作時,每一個Action都需要編寫一個類,並且要在struts-config.xml進行配置.這對於一個擁有很多Action的Web程序來說,工作量是非常大的.為此,Struts提供了DispatchAction類,這個類允許將一個Action作為一個方法來調用.在Web瀏覽器中通過請求參數來指定要調用的動作.

雖然DispatchAction類是一個抽象類,但其中卻沒有一個抽象方法.因此,DisplatchAction的子類不用實現任何DisplatchAction類中的方法.但如果要處理Action代碼,就必須根據相應的Action來編寫Action方法.一個Action方法除了方法名和execute方法不一樣外,其他的都和execute方法完全一樣.但編寫Action方法時要注意,Action方法名必須和用於指定動作的請求參數值一致(大小寫也必須一致).在下面的例子中演示了通過DispatchAction類實現方法和Action的對應.在這個例子中,有三個方法:fr、en和unspecificed.其中fr和en是兩個Action方法,分別用於將當前頁面轉發到法文和英文頁面,而當用於指定Action的請求參數不存在時,則調用unspecificed方法(在這個方法中將當前頁面轉發到中文頁面).在這個程序中使用的用於指定Action的請求參數為language(讀者可以指定自己的請求參數).

在<samples工程目錄>srcaction目錄建立一個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作為訪問參數的情況時調用這個方法

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參數表示用於指定Action的請求參數名.

在啟動Tomcat后,可通過如下的URL進行測試:

顯示英文頁面:

http://localhost:8080/samples/locale.do?language=en

顯示法文頁面:

http://localhost:8080/samples/locale.do?language=fr

顯示中文頁面(默認頁面):

http://localhost:8080/samples/locale.do

雖然上面的代碼可以很好地調用相應的Action方法,但在一些情況時,如請求參數language指定的Action方法不存在時,就會拋出異常.那麼如果我們想在非正常情況下都調用默認的處理Action動作的方法(也就是unspecificed方法)該怎麼辦呢?

實現上,實現這個功能也非常簡單,只要我們知道在什麼條件下調用unspecified方法,然後在非正常情況下,都將條件設為調用unspecified方法的條件就可實現這個功能.在查看DispatchAction類的源代碼后,可找到如下的代碼片段:

if (name == null) // name表示Action方法名
{
return this.unspecified(mapping, form, request, response);
}

從上面的代碼可知,只有當name為null時才會調用unspecified方法.這個name值實際上也就是language參數的值.也就是說,只有當language參數不存在時,name才會為null.如果在language的參數值所指的Action方法不存在時或者name為空串的情況下都將name設為null,那麼就可以達到我們的目的.

在DispatchAction類中有一個dispatchMethod方法,可以在這個方法中處理請求參數值為空串(也就是當「language=」時將方法名設為null)和Action方法未找到的情況.在Action類中有兩個特殊方法:execute和perform.如果調用了這兩個方法,將會出現遞歸調用的情況.因此,在調用這兩個方法時也需要將方法名設為null.這個工作可以在DispatchAction類的getMethodName方法中實現.為了完成這個功能,需要將上面的代碼放到MyDispatchAction類中.

protected ActionForward dispatchMethod(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response, String name)
throws Exception
{
ActionForward af
= null;
// 在language參數值為空串的情況下,將方法名賦為null
if (name != null) // name表示Action方法名,也是language的參數值
if (name.equals(""))
name
= null;
try
{
af
= super.dispatchMethod(mapping, form, request, response, name);
}
catch(NoSuchMethodException e) // 處理Action方法未找到的情況

{
// 在language的參數值沒有對應的Action方法時,將方法名賦為null
name = null;
af
= super.dispatchMethod(mapping, form, request, response, name);
}
return af;
}
// 當language的參數值為execute或perfore時,必須將方法名賦為null,否則會出現遞歸調用
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,直接將方法名設為null

return methodName;
}

現在我們可以用任何請求參數來訪問locale動作,只要未找到Action方法,就會調用默認的unspecified方法.讀者可以使用如下的URL來實驗一下:

http://localhost:8080/samples/locale.do?language=

http://localhost:8080/samples/locale.do?language=unknown


[火星人 ] Struts1.x系列教程(18):使用DispatchAction類調用多個Action方法已經有667次圍觀

http://coctec.com/docs/java/show-post-61812.html