[英]Using Spring MVC 3.1+ WebApplicationInitializer to programmatically configure session-config and error-page
[英]<error-page> setup doesn't work in Spring MVC
問題
我想向用戶顯示自定義錯誤頁面。 Simeply put,web.xml上的<error-page>
似乎不起作用。 我可以猜到問題是什么,但我需要了解為什么它不起作用。
我的設置
我在spring mvc應用程序中的web.xml上設置了<error-page>
。 以下是設置。
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
<url-pattern>*.*</url-pattern>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/error</location>
</error-page>
我的錯誤頁面就在...
WEB-INF
└ views
└ Errorpages
└ ErrorPage.jsp
對於你的信息,我也嘗試過以下這些東西,但這些都沒有用。
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/views/Errorpages/ErrorPage.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/views/Errorpages/ErrorPage.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/Errorpages/ErrorPage.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/ErrorPage.jsp</location>
</error-page>
<!-- Or tried those above without the .jsp that comes after ErrorPage -->
<!-- And also, tried those after moving the resources out of WEB-INF -->
第二個嘗試
我查看了服務器日志,發現spring試圖通過redirecion查找資源,所以我改變了我的計划,並試圖通過Spring MVC
典型視圖返回來實現這個目標。
我做了這個動作
@RequestMapping("/error")
public String throwsErrorPage(Locale locale, Model model) {
logger.info("Errorpage action..." + locale.getLanguage());
return "/Errorpages/ErrorPage";
}
我所期待的部分是正確的,因為它成功地稱之為行動。 我的服務器日志清楚地說明了。
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/notfound] in DispatcherServlet with name 'appServlet'
INFO : com.company.web.controller.HomeController - Errorpage action...ko
正如您所看到的,我通過在url上鍵入notfound來生成404錯誤,並且它重定向到/我在web.xml中的<location>
上指定的錯誤 。 但它不會返回視圖。
為了確保操作返回視圖,我嘗試更改操作的類型,如...
@RequestMapping("/error")
public ModelAndView throwsErrorPage(Locale locale, ModelAndView mv) {
logger.info("Errorpage action..." + locale.getLanguage());
return mv.setViewName("/Errorpages/ErrorPage");
}
但它不起作用......
奇怪的事情
如果我通過localhost:8080/error
調用該操作,它肯定會調用該操作並返回預期的錯誤頁面。 那么通過在url或ajax上鍵入內容,重定向和典型請求之間應該存在一些差異。
我想知道什么
<error-page>
配置工作? Spring
自動重定向與Spring MVC
用戶操作的典型請求之間有什么區別? 錯誤頁面大小必須> 1024字節。
添加一些無用的錯誤頁面進行測試。
添加此項以確保錯誤頁面具有以下屬性: [isErrorPage="true"]
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>
錯誤頁面可能不會放入web-inf,移出並嘗試很多
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>400</error-code>
<location>/index.jsp</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/403.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page
您必須將您的errorpages.jsp 放在WEB_INF文件夾之外,並將以下代碼放在您的web.xml中。
你能檢查你的servlet-context.xml配置嗎? 對我來說,它使用以下配置。
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
<url-pattern>*.*</url-pattern>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/error404</location>
</error-page>
和彈簧控制器方法
@RequestMapping(value = "/error404", method = RequestMethod.GET)
public String error() {
logger.info("in error method");
return "/errorPages/error404";
}
如果你想在web.xml中指定jsp的位置,你可以在那里指定,否則你可以添加url。 如果添加url,DispatcherServlet將負責給予瀏覽器響應,如果指定jsp,它將像普通的servlet響應一樣。
在servlet-context.xml中驗證這一點
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.