從 C++ 轉戰 Java、初學 Struts 2 時,最容易遇到的錯誤之一就是拼寫錯誤導致的配置失效。本文記錄一個真實案例:將 struts 誤寫為 structs,導致框架無法識別 Action 映射。
There is no Action mapped for namespace [/] and action name [user] associated with context path
瀏覽器訪問 /user 時拋出上述異常,控制台無其他異常堆疊,初學者極易誤以為是配置檔 struts.xml 或 Action 類別寫錯。
檢查 web.xml 中的 Filter 配置:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
若不小心將 org.apache.struts2... 寫成 org.apache.structs2...(少寫一個 't'),容器啟動時雖不會報錯(類別不存在只會在初始化時拋出 ClassNotFoundException,但若拼寫錯誤的類別剛好存在於其他套件中,則更隱蔽),但 Struts 核心初始化失敗,導致所有 Action 映射未註冊,進而拋出「no Action mapped」。

web.xml 中 <filter-class> 的完整類別名稱拼寫。struts2-core 版本正確且已下載。Loading configuration...)。mvn dependency:tree 確認 struts2-core 存在。Struts、ActionMapping、Configuration。一個字母之差,導致框架核心無法初始化。初學框架時,拼寫準確性與啟動日誌觀察同樣重要。遇到「no Action mapped」先查 Filter 配置類別名稱,再查 struts.xml 映射路徑,多半能快速定位。