首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Struts 1.x 系列教程(18):DispatchAction 整合多方法與預設處理增強

     編輯:Java 相關知識

前言

DispatchAction 允許單一 Action 類別包含多個業務方法,透過請求參數(預設 parameter 屬性指定參數名)決定呼叫哪個方法。本文示範基本用法、unspecified 預設處理,並進階解決「參數為空、方法不存在、遞迴調用」三大邊界問題。

基本實作:語系切換範例

Action 類別

package action;
import org.apache.struts.actions.DispatchAction;
import javax.servlet.*;
import javax.servlet.http.*;

public class MyDispatchAction extends DispatchAction {
    // 對應 language=fr
    public ActionForward fr(ActionMapping mapping, ActionForm form,
                            HttpServletRequest request, HttpServletResponse response) {
        forwardTo(request, response, "/newGlobal.jsp?language=fr");
        return null;
    }

    // 對應 language=en
    public ActionForward en(ActionMapping mapping, ActionForm form,
                            HttpServletRequest request, HttpServletResponse response) {
        forwardTo(request, response, "/newGlobal.jsp?language=en");
        return null;
    }

    // 無參數或參數無對應方法時調用
    protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
                                        HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        forwardTo(request, response, "/newGlobal.jsp?language=zh");
        return null;
    }

    private void forwardTo(HttpServletRequest request, HttpServletResponse response, String path) {
        try {
            RequestDispatcher rd = request.getRequestDispatcher(path);
            rd.forward(request, response);
        } catch (Exception e) {}
    }
}

配置

<action path="/locale"
        type="action.MyDispatchAction"
        parameter="language" />

parameter="language" 指定請求參數名。測試 URL:

  • /locale.do?language=en → 英文頁
  • /locale.do?language=fr → 法文頁
  • /locale.do → 中文頁(unspecified)

邊界問題與增強方案

問題場景

  • /locale.do?language=(空字串)→ 拋出 NoSuchMethodException
  • /locale.do?language=unknown(方法不存在)→ 同上
  • 參數值為 execute 或 perform → 觸發遞迴調用

解決方案:覆寫 dispatchMethod 與 getMethodName

// 處理空字串、方法不存在
protected ActionForward dispatchMethod(ActionMapping mapping, ActionForm form,
                                       HttpServletRequest request, HttpServletResponse response,
                                       String name) throws Exception {
    if (name != null && name.equals("")) name = null;  // 空字串視為無參數
    try {
        return super.dispatchMethod(mapping, form, request, response, name);
    } catch (NoSuchMethodException e) {
        name = null;  // 方法不存在轉向 unspecified
        return super.dispatchMethod(mapping, form, request, response, name);
    }
}

// 防止 execute/perform 遞迴
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;  // 強制轉向 unspecified
    }
    return methodName;
}

增強後行為

總結

DispatchAction 核心機制為「參數值即方法名」。掌握 unspecified、dispatchMethod、getMethodName 三個擴充點,即可構建健壯的多方法 Action,優雅處理各種異常參數情境。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
請求結果
/locale.do?language=中文頁(unspecified)
/locale.do?language=unknown中文頁(unspecified)
/locale.do?language=execute中文頁(避免遞迴)