[英]Spring MVC 404 Error Page
我使用ControllerAdvice定義了全局異常處理程序,對於IOException,我返回404。
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(IOException.class)
public void handleIOException(){
logger.error("IOException");
//returning 404 error code
}
web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/spring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/resources/404.jsp</location>
</error-page>
我故意在控制器中拋出IOException,但是它沒有使用404錯誤頁面。 如果我提供一些隨機網址,則表示正在使用404頁。
我很困惑為什么它不能與異常處理程序一起使用。
在我從tomcat webapps中刪除該項目並進行了全新的部署之后,現在看來一切正常。 可能是tomcat熱部署的一些問題。
因此,當我的控制器拋出404錯誤時,tomcat容器正在查看為其配置的錯誤頁面並發送配置的錯誤頁面作為響應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.