首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

Struts 2 配置 redirectAction 報錯:動態方法調用不支援的解決辦法

     編輯:程式設計疑難解答

錯誤現象

Action EmployerAction!emplyerList cannot be found in the namespace /,同類錯誤出現在多個 redirectAction 結果中。

原因

Struts 2.3+ 版本預設關閉 **動態方法調用**(Dynamic Method Invocation),即 ActionName!methodName 語法被禁止。

解決方案

  1. 開啟動態方法調用(不推薦生產環境):
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    在 struts.xml 的 <package> 根節點或 struts.properties 中配置。
  2. 推薦:改用通配符映射:
    <action name="EmployerAction_*" class="com.bzu.action.EmployerAction" method="{1}">
        <result name="success">/jsp/admin/employerlist.jsp</result>
        <result name="deletesuccess" type="redirect">EmployerAction_emplyerList</result>
    </action>
    URL 訪問 /EmployerAction_emplyerList.action 自動路由至 emplyerList() 方法,redirect 類型直接寫目標 Action 名稱。
  3. 或使用 REST 插件:struts2-rest-plugin 支援 RESTful 風格映射。

最佳實踐

避免在 redirectAction 中使用 ! 語法,改用 redirect + 完整 Action 名稱,或採用通配符/REST 映射,提升安全性與可維護性。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved