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.
ClassNotFoundException: FilterDispatcher 表示 struts2-core-x.x.x.jar 未部署至 WEB-INF/lib。FilterDispatcher,改用 StrutsPrepareAndExecuteFilter。若 web.xml 仍舊寫法會找不到類別。source 警告顯示專案未正確發布至 Tomcat 的 wtpwebapps 目錄。| 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>
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 已勾選部署。
Use Tomcat installation(需先停止 Server)。Dynamic Web Module、Java、JavaScript 已勾選。日誌顯示系統時間為 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 Assembly | Eclipse 熱部署需在 Deployment Assembly 註冊,否則不會複製到 wtpwebapps |
| 忽略 source 警告 | 該警告暗示專案未正確發布,類別路徑為空 |
Tomcat 啟動報 404 且 Struts2 Filter ClassNotFoundException,核心原因通常是 web.xml Filter 類別與 Struts 版本不符、相依 JAR 未部署至 WEB-INF/lib、Eclipse Deployment Assembly 設定缺失。依版本修正 web.xml、補齊所有 JAR、修正發布組態、同步系統時間,即可解決。