1. 程式人生 > >spring mvc Could not resolve view 返回500問題

spring mvc Could not resolve view 返回500問題

問題:
spring mvc找不到view時,返回http 500錯誤。

不是應該是404錯誤麼?

後來查原始碼發現,是因為專案裡用了多個HandlerMapping,其中包含了SimpleUrlHandlerMapping,而且設定了defaultHandler=UrlFilenameViewController,因此即使模板檔案不存在,也會繼續執行下去,直到丟擲javax.servlet.ServletException: Could not resolve view with name 'xxx' in servlet with name 'springServlet',否則的話,spring mvc找不到對應的handler時,會設定http狀態為404。

具體設定404的方法:
protected void noHandlerFound(HttpServletRequest request, HttpServletResponse response) throws Exception {
if(pageNotFoundLogger.isWarnEnabled()) {
pageNotFoundLogger.warn("No mapping found for HTTP request with URI [" + getRequestUri(request) + "] in DispatcherServlet with name \'" + this.getServletName() + "\'");
}

response.sendError(404);
}


[color=red]解決辦法麼,當前是去掉defaultHandler,但是隨之而來的問題,就是以前直接根據模板檔案查詢的url就不能用了,都需要對應的handler。[/color]