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

springboot自定义错误页面

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

springboot自定义错误页面

Spring Boot默认的错误页面路径是在resources/templates/error/目录下,当发生404、500状态码时,默认会跳转到该路径下的页面。

也可以自定义错误页面:

配置类
@Configuration
public class ErrorConfig implements ErrorPageRegistrar {

    @Override
    public void registerErrorPages(ErrorPageRegistry registry) {
        ErrorPage errorPage_404 = new ErrorPage(HttpStatus.NOT_FOUND,"/error_404");
        ErrorPage errorPage_500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR,"/error_500");

        registry.addErrorPages(errorPage_404, errorPage_500);
    }
}
处理器
@Controller
public class ErrorController {
    @GetMapping("/error_{status}")
    public String errorpage1(@PathVariable("status") String status) {
//        System.out.println("进入错误处理页面"+status);
		//这里返回的是页面的位置
        return "error/"+status;
    }
}

源码中springboot的web资源默认扫描位置是在一下四个路径下。

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{
	"classpath:/META-INF/resources/", 
	"classpath:/resources/", 
	"classpath:/static/", 
	"classpath:/public/"
};

所以error/ + status 实际上他会找到public中的error文件夹。同理,放在static,或者resources等文件夹下都可以

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/937008.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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