[英]Spring MVC 404 Error Page
I have Global exception handler defined using ControllerAdvice, for IOException, I am returning 404. 我使用ControllerAdvice定义了全局异常处理程序,对于IOException,我返回404。
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(IOException.class)
public void handleIOException(){
logger.error("IOException");
//returning 404 error code
}
web.xml file 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>
I am deliberately throwing IOException in a controller, but it's not using the 404 error page. 我故意在控制器中抛出IOException,但是它没有使用404错误页面。 If I give some random URL, the 404 page is getting used. 如果我提供一些随机网址,则表示正在使用404页。
I am confused why it's not working with exception handler. 我很困惑为什么它不能与异常处理程序一起使用。
It seems to be working fine now, after I deleted the project from tomcat webapps and did a fresh deploy. 在我从tomcat webapps中删除该项目并进行了全新的部署之后,现在看来一切正常。 May be some issues with tomcat hot deployment. 可能是tomcat热部署的一些问题。
So when my controller is throwing 404 error, tomcat container is looking at the error-page configured for it and sending configured error-page as response. 因此,当我的控制器抛出404错误时,tomcat容器正在查看为其配置的错误页面并发送配置的错误页面作为响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.