SpringBoot 配置404页面
项目环境:
服务器:centos
前端:Vue
后端:SpringBoot
出现的问题
访问一个不存在的页面时,会出现tomcat自带的404界面,这个界面对于用户不太友好
解决方案
1、自己写好404页面或者可以去找模板,分享出我自己的404简单模板
<!DOCTYPE html>
<html lang="en">
<head>
<title>404指定的请求路径不存在</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>body{font-family:"Microsoft YaHei";}a{text-decoration: none;color: #cf9236;}a:link{}a:hover{}a:visited{}a:focus{}.content{width: 800px;height: 400px;background-color: #0077aa;margin: auto;position: absolute;top: 50%;left: 50%;transform: translate(-50%,-50%);box-shadow: 10px 10px 5px #888;}.content .errorTitle{height: 100px;font-size: 30px;line-height: 100px;text-align: center;border-bottom: 4px solid #ffffff;}.content .errorTitle .bigSpan{font-size: 60px;margin-right: 20px;font-weight: bold;}.content .errorContent{height: 300px;}.content .errorContent .left{float: left;height: 100%;width: 50%;}.content .errorContent .right{float: right;height: 100%;width: 50%;}.lt,.lc{margin-left: 60px;margin-top: 30px;}.lt{font-size: 20px;}.lc {font-size: 14px;}
</style>
<body><div class="content"><div class="errorTitle"><span class="bigSpan">404</span>很抱歉,你访问的路径不见了!</div><div class="errorContent"><div class="left"><p class="lt">可能的原因</p><p class="lc">1、网络信号不好</p><p class="lc">2、找不到请求页面</p><p class="lc">3、小可爱被外星人绑走了</p></div><div class="right"><p class="lt">可以尝试</p><p class="lc"><a href="http://aierxing.cn:9000"> 返回首页 </a></p><p class="lc"><a href="mailto:44351030@163.com">联系管理员</a></p><p class="lc">Emmm......</p></div></div></div></body>
</html>
2、写一个类实现 ErrorPageRegistrar类
@Component
public class MyErrorPage implements ErrorPageRegistrar {@Overridepublic void registerErrorPages(ErrorPageRegistry registry) {/*1、按错误的类型显示错误的网页*//*错误类型为404,找不到网页的,默认显示404.html网页*/ErrorPage e404 = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");/*错误类型为500,表示服务器响应错误,默认显示500.html网页*/ErrorPage e500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");/*2、按具体某个异常显示错误的网页*//*当某个异常即可以根据错误类型显示错误网页,由可以根据某个具体的异常来显示错误网页时,优先根据具体的某个异常显示错误的网页*/ErrorPage argsException = new ErrorPage(IllegalArgumentException.class, "/args.html");registry.addErrorPages(e404, e500);}
}
3、静态资源拦截配置下
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");