1、在web.xml中追加
<error-page> <error-code>404</error-code> <location>/404</location> </error-page> <error-page> <error-code>500</error-code> <location>/500</location> </error-page>
*注意斜线 /
2、追加一个控制器
package io.deolin.controller; import javax.servlet.http.HttpServletRequest; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class ErrorPageController { Logger log = LogManager.getLogger(ErrorPageController.class); @RequestMapping("404") public String page404() { log.info("HTTP-400错误"); return "HTTP-404"; } @RequestMapping("500") public String page500() { log.error(500); return "HTTP-500"; } }
*注意请求URI保持一致
可以访问一个映射不到请求方法和静态资源的URL来测试一番,比如
http://localhost:8080/impossibleeeeeeeeeeeeeeeeeeeee
然后浏览器的页面内容就是HTTP-404.html的内容了。
时间: 2024-11-08 19:24:16