前言
Struts 1.x 透過 `struts-config.xml`、`ApplicationResources.properties` 等配置驅動,大多數異常源於配置與代碼不一致。本文整理 11 大高頻異常,附帶定位技巧。
異常清單與解決方案
1. "No bean found under attribute key XXX"
原因:`struts-config.xml` 定義 `` 的 `type` 類不存在、拼寫錯誤,或 `` 引用不存在的 form-bean。
解決:檢查類全名、套件路徑、JAR 是否在 `WEB-INF/lib`。
2. "Cannot find bean XXX in any scope"
原因:JSP `` 或 `request.getAttribute("xxx")` 取不到物件。
排查:
- Action 是否 `request.setAttribute("xxx", obj)` 並正確 forward
- Scope 是否匹配 (request/session/application)
- `` 內部 `` 的 `id`/`name` 一致性
3. "Missing message for key "XXX""
原因:`` 在 `ApplicationResources.properties` 找不到鍵值。
解決:
- 確認 properties 檔編碼 (ISO-8859-1 需 `native2ascii` 轉義)
- 多模組專案:`` 的 `message-resources` 參數指定正確檔名
4. "No getter method for property XXX of bean YYY"
原因:JSP `` 對應的 `getWrongProp()` 不存在。
解決:檢查拼寫 (如 `cade` → `code`)、Getter 命名規範、Boolean 型是否用 `isXxx()`。
5. "Cannot find ActionMappings or ActionFormBeans collection"
原因:`ActionServlet` 初始化失敗,導致配置集合未載入。常見於 `struts-config.xml` 遺失或解析錯誤。
6. "Cannot retrieve mapping for action /XXX"
原因:JSP `