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. oracle练习-day03

    .创建表空间.创建用户赋权限.创建表语法:.常见的数据类型字符             myname ) varchar2:推荐使用这个 可变长度最大字符    myname varchar2() 字 ...

  2. android:showAsAction

    在res/layout/menu文件夹下,放置login.xml: <menu xmlns:android="http://schemas.android.com/apk/res/an ...

  3. Day3-H-Alice and Bob HDU4268

    Alice and Bob's game never ends. Today, they introduce a new game. In this game, both of them have N ...

  4. Day2-K-Red and Black-HDU1312

    There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...

  5. 「PA2014」Kuglarz

    传送门 memset0好评 解题思路 其实这是一道图论题 不难发现,如果知道了 \(\sum1...i\) 和 \(\sum1...j\) 的奇偶性,那么就可以得知 \(\sum i+1...j\) ...

  6. 学习:java原理—反射机制

      一.什么是反射:反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力.这一概念的提 出很快引发了计算机科学领域关于应用反射性的研究.它首先被程 ...

  7. k-近邻算法的优缺点及拓展思考

    //2019.08.03晚#k-近邻算法的拓展思考与总结1.k-近邻算法是一种非常典型的分类监督学习算法,它可以解决多分类的问题:另外,它的整体思想简单,效果强大.它也可以用来解决回归问题,使用的库函 ...

  8. sessionManager配置

    在sessionManager配置的时候,有两个属性, 在这个类中,cacheManager是要注入到sessionDao中的,但要求sessionDao实现CacheManagerAware接口 C ...

  9. Prometheus Operator【转】

    前面我们介绍了 Kubernetes 的两种监控方案 Weave Scope 和 Heapster,它们主要的监控对象是 Node 和 Pod.这些数据对 Kubernetes 运维人员是必须的,但还 ...

  10. maze-----攻防世界

    题目下载之后在linux上查看一下 发现是elf文件尝试运行一下: 要求输入正确的flag才可以,ida查看 交叉引用 对长度和开头对比,进行判断. 转到400690查看 和#进行比较,hex 是一个 ...