整合線上編輯器 eWebEditor 後,圖片/附件上傳功能失效(無反應或 404),懷疑 Struts2 的 FilterDispatcher 攔截了上傳請求。
預設 web.xml 將 Struts2 映射至 /*:
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
導致所有請求(含 eWebEditor 的上傳 Servlet /ewebeditor/upload.jsp 等)先經 Struts2 處理,若無對應 Action 則回傳 404 或被安全攔截器阻擋。
將 Struts2 映射限定在特定前綴(如 /web/*):
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/web/*</url-pattern>
</filter-mapping>
同步調整:
/web/ 目錄下.action 請求路徑加上 /web/ 前綴/web/ 路徑,避開 Struts2 攔截Struts 2.1.6+ 支援 struts.action.excludePattern(struts.xml 或 struts.properties):
<constant name="struts.action.excludePattern" value="/ewebeditor/.*|/ckeditor/.*|/static/.*" />
或在 web.xml 使用 <dispatcher>REQUEST</dispatcher> 搭配 FilterChain 手動放行:
public class StrutsFilterExclude extends FilterDispatcher {
private static final String[] EXCLUDE_PREFIX = {"/ewebeditor/", "/static/"};
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
String uri = request.getRequestURI();
for (String prefix : EXCLUDE_PREFIX) {
if (uri.startsWith(request.getContextPath() + prefix)) {
chain.doFilter(req, res); // 直接放行
return;
}
}
super.doFilter(req, res, chain);
}
}
然後在 web.xml 替換 filter-class 為自訂類別。
excludePattern 配置,零代碼入侵。/web/*)最穩妥。