經典的 HTTP 404 錯誤:There is no Action mapped for namespace / and action name xxx。意味著 Struts2 找不到對應的 Action 映射。本文歸納三大核心排查方向,直擊痛點。
HTTP Status 404 - There is no Action mapped for namespace / and action name first. type Status report message There is no Action mapped for namespace / and action name first. description The requested resource is not available. Apache Tomcat/6.0.37
sturts.xml、struts2.xml → 必須命名為 struts.xml。src/main/resources),編譯後在 WEB-INF/classes 下。WEB-INF、WEB-INF/lib、包目錄下均無效。<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"https://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!-- package name 唯一,namespace 必須以 / 開頭 -->
<package name="default" namespace="/" extends="struts-default">
<action name="first" class="com.example.FirstAction">
<result>/success.jsp</result>
</action>
</package>
</struts>
/ 開頭,且與請求 URL 匹配(如 /user/* 對應 namespace="/user")。.action 後綴(除非配置了 struts.action.extension)。struts2-core、xwork、ognl、freemarker、commons-logging、commons-fileupload、commons-io、commons-lang3、javassist。target/classes/struts.xml 是否存在且內容正確。https://localhost:8080/你的項目/first.action(或配置的 action 名)。struts.devMode=true(struts.properties),控制台會打印詳細映射表。| 誤區 | 正確做法 |
|---|---|
| 以為 action 名包含 .action | struts.xml 中 name="first",URL 訪問 first.action |
| namespace 寫成空字符串 | 根命名空間寫 namespace="/" |
| 以為掃描注解不需要 struts.xml | 仍需 struts.xml 配置 <package>,或用 convention 插件 |
# 1. 確認文件存在
ls -la src/main/resources/struts.xml
# 2. 確認編譯輸出
ls -la target/classes/struts.xml
# 3. 檢查 namespace 和 action name
grep -n "namespace\|action name" src/main/resources/struts.xml
# 4. 檢查核心 jar 包版本
mvn dependency:tree | grep -E "struts2|xwork|ognl"