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

Struts2 註解配置 Action 訪問 404 無報錯:Namespace 與 URL 不匹配

     編輯:程式設計綜合問答

問題代碼

@Namespace(value = "msgAction")
@Action(value = "msg", results = { ... })
public class MsgAction extends ActionSupport {
    public String getInfo() { ... }
}

訪問 https://localhost:8080/AppServer_1.1/msgAction/msg.do 無反應、無報錯、不進方法。

根因分析

  • @Namespace("msgAction") 對應 URL 路徑 /msgAction/;
  • @Action("msg") 對應 msg(含 .do 後綴由 struts.action.extension 定義);
  • 完整映射路徑:**/msgAction/msg.do** —— **與用戶 URL 一致**;
  • 但 struts.xml 中 <package name="msg"> 未包含註解掃描包路徑,且註解掃描需 struts.convention.action.packages 或 struts.convention.package.locators 配置;
  • Spring 管理 Action 時需 @Controller + @Scope("prototype"),且 Struts2 需 struts.objectFactory=spring。

修正建議

  1. 在 struts.xml 添加約定插件配置:
<constant name="struts.convention.action.packages" value="com.AppServer.action" />
<constant name="struts.convention.package.locators" value="action" />
  1. 確保 struts2-convention-plugin JAR 在 classpath 中;
  2. Action 類加 @ParentPackage("convention-default") 繼承約定包;
  3. 檢查 web.xml Filter 映射 /* 是否攔截 .do;
  4. 啟用 struts.devMode=true 查看詳細日志。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved