springboot - mapping HTTP Response Status Codes to the custom JSP Error Page

1, Overview

2, the code

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>
View Code

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 Code

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 {
         the throw  new new ForbiddenException ( "Forbidden of The requested Page IS" ); 
    } 

    / ** 
     * returns 501, but can not navigate to the page 5xx.jsp 
     * / 
    @RequestMapping ( "/ some" ) 
    @ResponseStatus (HttpStatus.NOT_IMPLEMENTED) 
    public  void handleSomeRequest () { 
    } 
    / ** 
     * returns 501, but can not navigate to the page 5xx.jsp 
     * / 
    @RequestMapping ( "/ some2" )
     public ResponseEntity <?> handleSomeInfoRequest () { 
        ResponseEntity <?> = Re new 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

http status of all starting with 5 return code will navigate to the 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>
View Code

5)、404.jsp

Only 404 of the return code will navigate to 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>
View Code

6), error.jsp (folder in the views, i.e. the application.properties spring.mvc.view.prefix = / WEB-INF / views /)

In addition to 5xx, all state codes are outside the 404 to navigate to the page 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>
View Code

 

3, execution

 

 

 

Found, no navigation to 5xx.jsp, only 501 returned

  Enter a non-existent resource, navigate to the 404.jsp.

 

to sum up:

views

| _error.jsp [ error.jsp constant stored in the root, i.e. the application.properties spring.mvc.view.prefix = / WEB-INF / views / ]

| _error [ other error page must be stored in the error catalog ]

  |_5xx.jsp

  |_404.jsp

Guess you like

Origin www.cnblogs.com/yaoyuan2/p/11873110.html