首页 > 编程语言 > 详细

SpringBoot学习11:springboot异常处理方式1(自定义异常页面)

时间:2019-02-12 19:17:01      阅读:141      评论:0      收藏:0      [点我收藏+]

SpringBoot 默认的处理异常的机制:SpringBoot 默认的已经提供了一套处理异常的机制。一旦程序中出现了异常 SpringBoot 会向/error 的 url 发送请求。在 springBoot 中提供了一个叫 BasicExceptionController 来处理/error 请求,然后跳转到默认显示异常的页面来展示异常信息。

技术分享图片

如 果 我 们 需 要 将 所 有 的 异 常 同 一 跳 转 到 自 定 义 的 错 误 页 面 , 需 要 在src/main/resources/templates 目录下创建 error.html 页面。注意:名称必须叫 error

自定义错误页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>自定义错误页面</title>
</head>
<body>
    页面出错了。。。请与管理员联系
    <span th:text="${message}"></span>  <!--发生错误的信息-->
    <span th:text="${error}"></span>
</body>
</html>

编写controller

package com.bjsxt.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by Administrator on 2019/2/12.
 */
@Controller
public class IndexController {


    @RequestMapping("toIndex")
    public String toIndex(){
        String str=null;
        str.length();
        return "index";
    }

    @RequestMapping("toIndex2")
    public String toIndex2(){
        int num=10/0;
        return "index";
    }

}

技术分享图片

 

SpringBoot学习11:springboot异常处理方式1(自定义异常页面)

原文:https://www.cnblogs.com/duanrantao/p/10366691.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!