Struts 設定檔中 Action 定義:
<action path="/docreateuser" type="com.zchome.CreateUserAction"
name="CreateUserForm" scope="request" input="createuser">
<forward name="createuser" path="/jsp/createuser.jsp"/>
</action>
現象:Validator 驗證通過;但 ActionForm validate() 失敗時,拋出 IllegalArgumentException: Path createuser does not start with a "/" character。
// RequestProcessor.java (Struts 1.1)
protected boolean processValidate(HttpServletRequest request, HttpServletResponse response,
ActionForm form, ActionMapping mapping) throws IOException, ServletException {
if (form == null) return true;
// 執行驗證
ActionErrors errors = form.validate(mapping, request);
if (errors != null && !errors.isEmpty()) {
// 驗證失敗,導向 input 指定路徑
request.setAttribute(Globals.ERROR_KEY, errors);
// 關鍵行:
return processForward(request, response, mapping.getInput());
}
return true;
}
protected boolean processForward(HttpServletRequest request, HttpServletResponse response,
String forward) throws IOException, ServletException {
if (forward == null) return false;
// 內部呼叫 internalModuleRelativeForward()
return internalModuleRelativeForward(request, response, forward);
}
protected boolean internalModuleRelativeForward(HttpServletRequest request,
HttpServletResponse response, String path)
throws IOException, ServletException {
// 關鍵檢查:路徑必須以 "/" 開頭
if (!path.startsWith("/")) {
throw new IllegalArgumentException("Path " + path + " does not start with a '/' character");
}
// ... 取得 RequestDispatcher 並 forward
}
mapping.getInput() 返回的是 struts-config.xml 中 input="createuser" 的原始值。internalModuleRelativeForward() 要求路徑必須以 / 開頭,但設定值未加斜線,導致異常。
在 struts-config.xml 中修正 input 屬性:
<action path="/docreateuser" type="com.zchome.CreateUserAction"
name="CreateUserForm" scope="request" input="/jsp/createuser.jsp">
<forward name="createuser" path="/jsp/createuser.jsp"/>
</action>
或直接使用 forward 名稱(需 Struts 1.1+ 支援):
<action ... input="createuser" /> <!-- 僅當 input 對應 forward name 時有效,否則需完整路徑 -->
/ 開頭的絕對路徑 作為 input、forward 的 path 值/jsp/、/tiles/ 等,便於維護<global-exceptions> 設定未捕獲異常導向| 類別 | 方法 | 職責 |
|---|---|---|
| RequestProcessor | processValidate() | 執行表單驗證,失敗時導向 input |
| RequestProcessor | processForward() | 處理前導轉向 |
| RequestProcessor | internalModuleRelativeForward() | 模組相對路徑轉向,檢查 "/" 前綴 |
| TilesRequestProcessor | doForward() | Tiles 整合下的轉向實作 |
input 屬性值必須為以 / 開頭的模組相對路徑,否則驗證失敗時會拋出 IllegalArgumentException。此為 Struts 1.1 核心轉向機制的強制要求,開發者需養成絕對路徑習慣。