栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

3.7SpringBoot之异常页面

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

3.7SpringBoot之异常页面

异常页面分为静态(static)和动态(templates),不论是静态还是动态都必须在新建一个error目录

1.静态

404.html/500.html

这种是具体到某一种具体的异常

4xx.html/500.html

这种是从400-499/500-599所有类型的异常

静态一般都是直接在页面写一句话,或者一个字符串,信息相对简单

例如




    
    Title


500错误


 

2.动态(thymeleaf模版,可以得到异常的相关信息)




    
    Title


templates-5xx
path
timestamp
message
error
status

 也可以增加异常内容

在前台页面增加


        myerror
        
    

然后增加配置类MyErrorAttribute

@Component
public class MyErrorAttribute extends DefaultErrorAttributes {
    @Override
    public Map getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
        Map map = super.getErrorAttributes(webRequest, includeStackTrace);
        map.put("myerror", "这是我自定义的异常信息!");
        return map;
    }
}

 自定义视图


@Component
public class MyErrorViewResolver extends DefaultErrorViewResolver {
    
    public MyErrorViewResolver(ApplicationContext applicationContext, ResourceProperties resourceProperties) {
        super(applicationContext, resourceProperties);
    }

    @Override
    public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map model) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("java666");
        mv.addAllObjects(model);
        return mv;
    }
}



    
    Title


java666-5xx
path
timestamp
message
error
status
myerror

 

自定义视图用的很少,而且SpringBoot2.6以上总是有一个地方报错,我也不知道为啥

 

 异常页面优先级

先精确,后模糊。 先动态,后静态

例如:404页面
templates/error/404.html
static/error/404.html
templates/error/4xx.html
static/error/4xx.html
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/863319.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号