Struts 1.x 專案中,JSP `
| 請求方式 | JSP 寫法 | struts-config.xml 對應 | 匹配規則 |
|---|---|---|---|
| 表單提交 | <html:form action="/clsCompCaseDealAction"> | <action path="/clsCompCaseDealAction" ...> | action 值必須與 path 完全一致(含前導 /) |
| 超連結/重導向 | <a href="/module/xxx.do"> | <action path="/xxx" ...> | 以「當前請求路徑的虛擬目錄」為基準做相對解析 |
Struts 1 使用「模組前綴」+「action path」組成完整 URL。假設:
/myapp/moduleA(在 `path="/user/login"則實際可訪問 URL 為:https://host:port/myapp/moduleA/user/login.do
登入
登入
登入
登入
因虛擬目錄存在,靜態資源若用相對路徑極易 404。統一使用:
<base href="<%=request.getScheme()%>://<%=request.getServerName()%>:<%=request.getServerPort()%><%=request.getContextPath()%>/">
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<link rel="stylesheet" href="${ctx}/css/main.css"/>
<script src="${ctx}/js/app.js"></script>
| 現象 | 可能原因 | 檢查重點 |
|---|---|---|
| 表單提交 404 | form action 與 action path 不一致 | 大小寫、前導 /、模組前綴 |
| 超連結 404 | 相對路徑解析錯誤 | 改用絕對路徑或 `${ctx}` |
| CSS/JS 404 | 虛擬目錄導致相對路徑偏移 | 加 ` |
| Action 不執行 | struts-config.xml 未載入或模組未註冊 | web.xml ` |
Struts 1 已停止維護(EOL 2013),新專案建議直接使用 Spring MVC / Struts 2 / Spring Boot。若必須維護舊專案,請嚴守「form action = action path」與「靜態資源用絕對路徑」兩大鐵律,可避免 90% 的路徑問題。