Spring Boot自定义错误视图

方式3. 自定义实现错误视图解析,统一错误处理

如果不想要使用缺省的错误处理视图解析器,想要定制一些自己的东西(比如说:错误引导信息等),按照官方文档的建议我们可以自定义实现错误视图解析接口来处理。

下面就是通过实现错误视图解析接口ErrorViewResolver,将4xx、5xx的错误页面集中在一个自定义视图上:

1)实现 ErrorViewResolver 接口

package com.hongyang.admin.web;import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;import org.springframework.http.HttpStatus;import org.springframework.stereotype.Component;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import java.util.Map;/** * 实现自定义的错误视图解析器

*/@ComponentpublicclassAdminErrorViewResolverimplements ErrorViewResolver {

    /**    * 实现ErrorViewResolver约定方法,

    * 返回统一的错误视图.

    * @param request

    * @param status

    * @param model

    * @return*/    @Override

    publicModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map model) {

        returnnewModelAndView("/error/index", model);

    }

}

正在上传... 取消

2)完成错误视图,在templates/error下添加index.ftlh视图(freemarker模板)

正在上传... 取消

<!DOCTYPE html><html><head ><link href="/content/public/images/logo-small.png" rel="shortcut icon"/><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>${status}</title><meta name="renderer" content="webkit"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="apple-mobile-web-app-status-bar-style" content="black"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="format-detection" content="telephone=no"><style>        body {            position: fixed;            z-index: 10;            top: 0;            left: 0;            right: 0;            bottom: 0;            width: 100%;            padding: 0;            margin: 0px;            font-size: 14px;            background: #fff;            word-wrap: break-word;}        p {            padding: 0 15px;}        .btn {            border: 0px;            color: #fff;            cursor: pointer;            text-align: center;            background-color: #ff7a5f\0;            box-shadow: #cccccc 0 2px 15px 0;            -webkit-box-shadow: 0 2px 7px 0 rgba(0,0,0,0.2);            background: radial-gradient(circle at 300% 50%, rgb(255, 195, 114) 50%, rgb(255, 105, 90) 100%);            transition: all .2s ease-out,box-shadow .2s ease-out;}        .btn:hover {            color: #FFFFFF;            transform: scale(1.1);}        .btn:focus {            outline: none;}        .container {            margin: 8% auto;}        .container p {            margin: 35px auto;            text-align: center;}        .container img {            width: 20%;}        .container .font {            color: #848484;}        .container .btn-back {            width: 180px;            height: 35px;            border-radius: 6px;}        #container-info {            display: none;            position: fixed;            z-index: 11;            top: 5%;            left: 0;            right: 0;            margin: 0 auto;            width: 65%;            height: 85%;            overflow: hidden;            border-radius: 4px;            border: 1px solid #f1986e;            background-color: #fff;            box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgb(242, 154, 110);}        #container-info .btn-close {            position: absolute;            right: 5px;            top: 5px;            width: 25px;            height: 25px;            line-height: 25px;            font-size: 22px;            padding: 2px;            border-radius: 50%;            text-align: center;}        #container-info p {            font-size: 12px;            line-height: 20px;}        .show {            display: block !important;}        .cor-r {            color: red;}        @media (max-width: 767px) {            .container img {

                width: 50%;}            #container-info {                width: 85%;}        }

        .panel-heading {            height: 32px;            line-height: 32px;            padding: 5px 15px;}        .panel-body {            height: calc(85vh - 40px);            overflow: auto;}        .panel-orange .panel-heading {            background-color: #ffa0681f;            color: #ff6f5c;}</style></head><body><form id="form1"><div class="container"><p><img src="/content/public/images/error_${status}.png"/></p><p class="font">${error},<a onclick="errorDetail(true)" href="javascript: void(0)">点击查看明细</a>!</p><p><button type="button" class="btn btn-back" id="btnBack">返回</button></p></div><div id="container-info"><span class="btn btn-close" onclick="errorDetail(false)">×</span><div class="panel panel-orange"><div class="panel-heading">                错误说明

            </div><div class="panel-body"><#if path??><p><b>请求的URL:</b>${path}</p></#if><#if message??><p><b>异常信息:</b><span class="cor-r">${message}</span></p></#if><#if trace??><p><b>StackTrace:</b><br>${trace}</p></#if></div></div></div><script type="text/javascript">        window.onload =function () {

            var btn = document.getElementById("btnBack");

            btn.onclick =function () {

                var url = document.referrer;

                if (url.indexOf("home/main") >0) {

                    window.parent.tabDelete();

                    return;

                }

                window.history.back(-1);

            }

        }

        function errorDetail(isShow) {

            var con = document.getElementById("container-info");

            con.className = isShow ?"show" : ""; // 兼容IE8        }

    </script></form></body></html>

Absorbing material: www.goodsmaterial.com

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,607评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,239评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,960评论 0 355
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,750评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,764评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,604评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,347评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,253评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,702评论 1 315
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,893评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,015评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,734评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,352评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,934评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,052评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,216评论 3 371
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,969评论 2 355