HOW-TO:带有Spring MVC的Tomcat中的自定义错误页面
默认的Tomcat错误页面看起来很可怕。 此外,它们可能会公开有价值的信息,包括服务器版本和异常堆栈跟踪。 Servlet规范提供了一种通过web.xml配置异常行为的方法。 可以配置对特定Java异常的响应,也可以配置对选定的Http响应代码的响应。
error-page
元素指定错误代码或异常类型与Web应用程序中资源路径之间的映射:
<web-app><!-- Prior to Servlet 3.0 define either an error-code or an exception-type but not both --><error-page><!-- Define error page to react on Java exception --><exception-type>java.lang.Throwable</exception-type><!-- The location of the resource to display in response to the error will point to the Spring MVC handler method --><location>/error</location></error-page><error-page><error-code>404</error-code><location>/error</location></error-page><!-- With Servlet 3.0 and above general error page is possible --><error-page><location>/error</location></error-page></web-app>
在我们的web.xml中定义了自定义错误页面后,我们需要添加Spring MVC @Controller
。 customError
处理程序方法包装我们从请求中检索的信息,并将其返回到视图。
@Controller
class CustomErrorController {@RequestMapping("error") public String customError(HttpServletRequest request, HttpServletResponse response, Model model) {// retrieve some useful information from the requestInteger statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception");// String servletName = (String) request.getAttribute("javax.servlet.error.servlet_name");String exceptionMessage = getExceptionMessage(throwable, statusCode);String requestUri = (String) request.getAttribute("javax.servlet.error.request_uri");if (requestUri == null) {requestUri = "Unknown";}String message = MessageFormat.format("{0} returned for {1} with message {3}", statusCode, requestUri, exceptionMessage); model.addAttribute("errorMessage", message); return "customError";}private String getExceptionMessage(Throwable throwable, Integer statusCode) {if (throwable != null) {return Throwables.getRootCause(throwable).getMessage();}HttpStatus httpStatus = HttpStatus.valueOf(statusCode);return httpStatus.getReasonPhrase();}
}
产生的消息可能如下所示: 404 returned for /sandbox/bad with message Not Found
。
要查看运行中的代码,请浏览Spring MVC Quickstart Archretype的源代码,或者更好的方法是使用它生成一个新项目。
翻译自: https://www.javacodegeeks.com/2013/11/how-to-custom-error-pages-in-tomcat-with-spring-mvc.html
HOW-TO:带有Spring MVC的Tomcat中的自定义错误页面相关推荐
- react 错误边界_React with GraphQL和错误边界中的自定义错误页面
react 错误边界 by Abi Noda 通过Abi Noda React with GraphQL和错误边界中的自定义错误页面 (Custom error pages in React with ...
- ASP.NET Core中显示自定义错误页面-增强版
之前的博文 ASP.NET Core中显示自定义错误页面 中的方法是在项目中硬编码实现的,当有多个项目时,就会造成不同项目之间的重复代码,不可取. 在这篇博文中改用middleware实现,并且放在独 ...
- 在Spring MVC应用程序中使用Bean Validation 1.1获得更好的错误消息
在许多新功能中, Bean Validation 1.1引入了使用统一表达式语言(EL)表达式的错误消息插值. 这允许基于条件逻辑来定义错误消息,还可以启用高级格式化选项 . 添加到Spring MV ...
- Spring MVC和REST中@RestController和@Controller注释之间的区别
Spring MVC中的@RestController注释不过是@Controller和@ResponseBody注释的组合. 它已添加到Spring 4.0中,以简化在Spring框架中RESTfu ...
- Spring MVC应用程序中的Thymeleaf模板布局,无扩展
在使用JSP / JSTL和Apache Tiles几年之后,我开始为我的Spring MVC应用程序发现Thymeleaf. Thymeleaf是一个非常出色的视图引擎,尽管目前缺乏良好的Intel ...
- spring mvc在Controller中获取ApplicationContext
spring mvc在Controller中获取ApplicationContext web.xml中进行正常的beans.xml和spring-mvc.xml的配置: 需要在beans.xml中进行 ...
- Spring MVC 在JSP中获取 Service或Dao
Spring MVC 在JSP中获取service 在Controller.Service层都已经配置好了自动注入,但是在JSP中直接使用 IuserInfoShService uishService ...
- java tomcat 404配置_在Tomcat中配置404自定义错误页面详解
一直使用tomcat,今天想到一个问题,自定义404错误页面, 为了获得很好的用户感受,是不应当向用户暴露404这样的页面的, 问题的出发点是我在Struts2中定义错误页面, 在Struts2中是这 ...
- Spring Boot自定义错误页面
Spring Boot以一种新的微服务的方式来替代以Spring Framework构建项目的传统方式,我已经计划在后续的项目开发中使用它.它已经帮我们做了90%的工作,剩下10%的工作需要我们自己去 ...
最新文章
- uva live 4394 String painter 间隔dp
- 干货收藏 | 计算机学习绝对不能错过的10大网站(内附网站链接)
- 八十四、Python | Leetcode回溯算法系列
- 程序的不同段的区别和作用
- WebRTC Audio Encoder/Decoder Factory 的实现
- 报错空指针异常_让你为之颤抖的Java常见的异常exception
- MySQL 优化 —— EXPLAIN 执行计划详解
- pycharm安装带激活码2018
- 参数展示初始三层架构
- Youtube Links
- 矩阵线性代数笔记整理汇总,超全面
- 网站在线沟通工具,网站即时聊天工具-TTKEFU在线客服系统功能介绍
- 量子加密神话破灭?破解论文出现
- 如何进行敌我识别(IFF)应用中的二次监视雷达(SSR)的模块化产线测试
- 天人合一物我相融,站点升级渐进式Web应用PWA(Progressive Web Apps)实践
- 从oracle到mysql模型转换的自动化实现
- Python 测试题(覆盖了大多数的基础知识和进阶)
- HiveSql常用的时间维度计算方法(月初、月末、周几)及时间维度 表生成
- 高版本Chrome网页直接播放海康威视大华RTSP流方案,不需服务器转码转流,支持H.265、H.264及2K/4K高清视频
- 华科计算机组成原理 头歌Educoder Logisim 运算器设计(HUST) 1~11关满分通关
热门文章
- IP暴露接口IP白名单设置
- php oracle 无查询结果,php - Oracle Insert查询不起作用,也不会抛出任何错误 - 堆栈内存溢出...
- ISO语言代码和国家代码+Locale常量+ISO货币符号
- kafka 发布-订阅模式_使用Apache Kafka作为消息系统的发布-订阅通信中的微服务,并通过集成测试进行了验证...
- aws ecr_在ECR上推送Spring Boot Docker映像
- aws sqs_在Spring中将AWS SQS用作JMS提供程序
- java jsp学习指南_JSP教程–最终指南
- 将旧对象装箱可自动关闭
- 使用Spring Data R2DBC进行异步RDBMS访问
- 使用Java流查询数据库