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

Tomcat 啟動報 404 與 Struts2 FilterDispatcher ClassNotFoundException 完整排查

     編輯:程式設計綜合問答

錯誤日誌關鍵資訊

SEVERE: Exception starting filter struts2
java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.FilterDispatcher
...
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:ChooseSeat' did not find a matching property.

核心問題診斷

  1. Struts2 核心 JAR 缺失:ClassNotFoundException: FilterDispatcher 表示 struts2-core-x.x.x.jar 未部署至 WEB-INF/lib。
  2. Struts2 版本不相容:Struts 2.5+ 已移除 FilterDispatcher,改用 StrutsPrepareAndExecuteFilter。若 web.xml 仍舊寫法會找不到類別。
  3. Eclipse 部署組態問題:source 警告顯示專案未正確發布至 Tomcat 的 wtpwebapps 目錄。

解決步驟

1. 確認 Struts2 版本與 web.xml 對應

Struts 版本web.xml Filter 類別
Struts 2.3.x 舊版org.apache.struts2.dispatcher.FilterDispatcher
Struts 2.5.x+ 新版org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter

修正 web.xml(以 2.5+ 為例):

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

2. 檢查 WEB-INF/lib 必備 JAR(Struts 2.5.22 為例)

struts2-core-2.5.22.jar
commons-fileupload-1.4.jar
commons-io-2.6.jar
commons-lang3-3.8.1.jar
freemarker-2.3.28.jar
ognl-3.2.1.jar
javassist-3.24.0-GA.jar
xwork-core-2.3.34.jar  (struts2-core 已包含,無需額外)

在 Eclipse 中:右鍵專案 → Properties → Deployment Assembly → 確認 Maven Dependencies 或手動加入的 JAR 已勾選部署。

3. 修正 Eclipse 專案發布設定

  1. Servers 視圖 → 雙擊 Tomcat → Server Locations 選 Use Tomcat installation(需先停止 Server)。
  2. 專案右鍵 → Properties → Project Facets → 確認 Dynamic Web Module、Java、JavaScript 已勾選。
  3. 專案右鍵 → Properties → Deployment Assembly → Add → Java Build Path Entries → Maven Dependencies。
  4. 清理重發布:Server 右鍵 Clean → Clean Tomcat Work Directory → Publish。

4. 排除系統時間影響

日誌顯示系統時間為 2015-01-01,若 JAR 簽名驗證涉及時間戳記可能造成驗證失敗。請同步系統時間至正確日期。

完整驗證清單

[ ] web.xml filter-class 與 Struts 版本匹配
[ ] WEB-INF/lib 包含 struts2-core 及所有相依 JAR
[ ] Eclipse Deployment Assembly 包含 Maven Dependencies
[ ] Server Locations 設為 Use Tomcat installation
[ ] 專案 Facets 正確
[ ] 系統時間正確
[ ] Tomcat 控制台無 ClassNotFoundException
[ ] 瀏覽器訪問 https://localhost:8080/專案名/actionName 正常

常見誤區

誤區事實
以為只要加 struts2.jar 就夠Struts 2.5+ 有 7+ 個強制相依 JAR,缺一不可
直接複製 JAR 到 WEB-INF/lib 不用管 Deployment AssemblyEclipse 熱部署需在 Deployment Assembly 註冊,否則不會複製到 wtpwebapps
忽略 source 警告該警告暗示專案未正確發布,類別路徑為空

總結

Tomcat 啟動報 404 且 Struts2 Filter ClassNotFoundException,核心原因通常是 web.xml Filter 類別與 Struts 版本不符、相依 JAR 未部署至 WEB-INF/lib、Eclipse Deployment Assembly 設定缺失。依版本修正 web.xml、補齊所有 JAR、修正發布組態、同步系統時間,即可解決。

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