1、总览

2、代码

1)、pom.xml

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency> </dependencies>

2)、application.properties

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
server.error.whitelabel.enabled=false
server.error.include-stacktrace=always

3)、java代码

package com.ebc.controller;

import com.ebc.error.ForbiddenException;
import com.ebc.error.NotYetImplemented;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus; @Controller
public class MyController {
/**
* response status code=500,导航到5xx.jsp
*/
@RequestMapping("/")
public void handleRequest() {
throw new RuntimeException("test exception");
}
/**
* response status code=501,导航到5xx.jsp
*/
@RequestMapping("/app")
public void handleAppInfoRequest() throws NotYetImplemented {
throw new NotYetImplemented("The request page is not yet implemented");
} /**
* response status code=403,因为没有403.jsp,因此会导航到error.jsp
*/
@RequestMapping("/admin")
public void handleAdminRequest() throws ForbiddenException {
throw new ForbiddenException("The requested page is forbidden");
} /**
* 返回501,但是无法导航到5xx.jsp页面
*/
@RequestMapping("/some")
@ResponseStatus(HttpStatus.NOT_IMPLEMENTED)
public void handleSomeRequest() {
}
/**
* 返回501,但是无法导航到5xx.jsp页面
*/
@RequestMapping("/some2")
public ResponseEntity<?> handleSomeInfoRequest() {
ResponseEntity<?> re = new ResponseEntity<>((Object)null, HttpStatus.NOT_IMPLEMENTED);
return re;
}
} package com.ebc.error; import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus; @ResponseStatus(HttpStatus.FORBIDDEN)
public class ForbiddenException extends Exception {
public ForbiddenException(String message) {
super(message);
}
} package com.ebc.error; import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus; @ResponseStatus(HttpStatus.NOT_IMPLEMENTED)
public class NotYetImplemented extends Exception {
public NotYetImplemented(String message) {
super(message);
}
}

4)、5xx.jsp

所有5开头的http状态返回码都会导航到该5xx.jsp。

<%@ page language="java"
contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<style>
table td{
vertical-align:top;
border:solid 1px #888;
padding:10px;
}
</style>
</head>
<body>
<h1>My 5xx Error Page</h1>
<table>
<tr>
<td>Date</td>
<td>${timestamp}</td>
</tr>
<tr>
<td>Error</td>
<td>${error}</td>
</tr>
<tr>
<td>Status</td>
<td>${status}</td>
</tr>
<tr>
<td>Message</td>
<td>${message}</td>
</tr>
<tr>
<td>Exception</td>
<td>${exception}</td>
</tr>
<tr>
<td>Trace</td>
<td>
<pre>${trace}</pre>
</td>
</tr>
</table>
</body>
</html>

5)、404.jsp

只有404的返回码会导航到404.jsp

<%@ page language="java"
contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<style>
table td{
vertical-align:top;
border:solid 1px #888;
padding:10px;
}
</style>
</head>
<body>
<h1>My 404 Error Page</h1>
<table>
<tr>
<td>Date</td>
<td>${timestamp}</td>
</tr>
<tr>
<td>Error</td>
<td>${error}</td>
</tr>
<tr>
<td>Status</td>
<td>${status}</td>
</tr>
<tr>
<td>Message</td>
<td>${message}</td>
</tr>
<tr>
<td>Exception</td>
<td>${exception}</td>
</tr>
<tr>
<td>Trace</td>
<td>
<pre>${trace}</pre>
</td>
</tr>
</table>
</body>
</html>

6)、error.jsp(在views文件夹下,即application.properties 中的spring.mvc.view.prefix=/WEB-INF/views/)

除了5xx、404外的所有状态码都会到航到error.jsp页面

<%@ page language="java"
contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<style>
table td{
vertical-align:top;
border:solid 1px #888;
padding:10px;
}
</style>
</head>
<body>
<h1>My Custom Global Error Page</h1>
<table>
<tr>
<td>Date</td>
<td>${timestamp}</td>
</tr>
<tr>
<td>Error</td>
<td>${error}</td>
</tr>
<tr>
<td>Status</td>
<td>${status}</td>
</tr>
<tr>
<td>Message</td>
<td>${message}</td>
</tr>
<tr>
<td>Exception</td>
<td>${exception}</td>
</tr>
<tr>
<td>Trace</td>
<td>
<pre>${trace}</pre>
</td>
</tr>
</table>
</body>
</html>

3、执行

发现,没有导航到5xx.jsp,只是返回了501

输入一个不存在的资源,导航到了404.jsp了。

总结:

views

|_error.jsp【error.jsp一定存放在根下,即application.properties 中的spring.mvc.view.prefix=/WEB-INF/views/】

|_error【其他错误页面一定存放在error目录下】

  |_5xx.jsp

  |_404.jsp

springboot - 映射 HTTP Response Status Codes 到自定义 JSP Error 页面的更多相关文章

  1. springboot - 映射HTTP Response Status Codes 到 静态 HTML页面

    1.总览 2.代码 1).pom.xml <dependencies> <dependency> <groupId>org.springframework.boot ...

  2. springboot - 映射HTTP Response Status Codes 到 FreeMarker Error页面

    1.总览 2.代码 1).pom.xml 这里注意:springboot 2.2.0以后默认的freemarker文件后缀为:ftlh.本例用的是2.2.1,所以后缀为ftlh <depende ...

  3. 【SpringBoot】SpringBoot拦截器实战和 Servlet3.0自定义Filter、Listener

    =================6.SpringBoot拦截器实战和 Servlet3.0自定义Filter.Listener ============ 1.深入SpringBoot2.x过滤器Fi ...

  4. SpringBoot整合WEB开发--(五)自定义错误页

    目的与原理: 处理异常时,若我们想根据实际情况返回不同的页面,@ControllerAdvice与@ExceptionHandler,一般用于处理应用级别的异常,一些容器级别的错误就处理不了,例如Fi ...

  5. HTTP状态码(HTTP Status codes)简介

    HTTP可能大家都熟悉,就是超文本传输协议.浏览器通过HTTP与WEB Server通讯(也有一些其它软件比如IM使用HTTP协议传递数据),把我们的请求(HTTP Request)传递给服务器,服务 ...

  6. HTTP response status

    The status code is a 3-digit number: 1xx (Informational): Request received, server is continuing the ...

  7. C#、JAVA操作Hadoop(HDFS、Map/Reduce)真实过程概述。组件、源码下载。无法解决:Response status code does not indicate success: 500。

    一.Hadoop环境配置概述 三台虚拟机,操作系统为:Ubuntu 16.04. Hadoop版本:2.7.2 NameNode:192.168.72.132 DataNode:192.168.72. ...

  8. IIS SMTP status codes

    Here are the meaning of SMTP status codes. Status Code Description 211 System status, or system help ...

  9. HTTP常见返回代码(HTTP Status codes)的分类和含义

    HTTP错误主要分成三类:用户设备问题.Web服务器问题和连接问题.当客户端向Web服务器发送一个HTTP请求时,服务器都会返回一个响应代码.而这些响应代码主要分成五类. HTTP状态码中定义了5大类 ...

随机推荐

  1. javaScript中this的指向?

    javaScript中this对象是在运行时基于函数的执行环境绑定的,在全局函数中,this等于window,而当函数被作为某个对象的方法调用时,this等于那个对象. 但在实际中,代码环境复杂,th ...

  2. dateDiff 用法

    SELECT DATEDIFF(yy,'2018-3-10 16:40:00',getdate())SELECT DATEDIFF(mm,'2018-3-10 16:40:00',getdate()) ...

  3. CSS-fontface

    @font-face { font-family: <identifier>; src: <fontsrc> [, <fontsrc>]*; <font> ...

  4. 发送短信-----用restful 校验规则

    我们的需求如下 1. 获取手机号 . 手机格式校验 . 生成随机验证码 . 验证码发送到手机上 . 把验证码+手机号保留(30s过期) 复习 我们在有表的情况下的序列化 class MessageSe ...

  5. 第1节 IMPALA:8、shell交互窗口使用;9、外部和内部shell参数

    impala当中的元数据的同步的问题impala当中创建的数据库表,直接就可以看得到,不用刷新hive当中创建的数据库表,需要刷新元数据才能够看得到 因为impala的catalog的服务,我们需要通 ...

  6. ROS学习笔记2-基本概念

    本笔记来源于:http://wiki.ros.org/ROS/Concepts ROS文件系统级别文件系统级别主要包含了你能在ROS的磁盘上遇到的资源,包括: 包(Packages):包是ROS中资源 ...

  7. Java笔记--网络编程

    1.IP地址:InetAddress类 --唯一的标识Internet上的计算机 --本地回环地址(hostAddress)127.0.0.1 主机名(hostName):localhost //根据 ...

  8. Thymeleaf(一)---引入js/css文件

    th:href="@{/static/css/style.css}" th:src="@{/static/js/thymeleaf.js}" index.htm ...

  9. Spark Deploy 模块

    Spark Scheduler 模块的文章中,介绍到 Spark 将底层的资源管理和上层的任务调度分离开来,一般而言,底层的资源管理会使用第三方的平台,如 YARN 和 Mesos.为了方便用户测试和 ...

  10. 1 初识JVM