Springboot-自定义错误页面(4xx/5xx)

此方法适用于项目打jar包的方式!!!

 

1、在config包下创建ErrorConfiguration类:

import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.boot.web.server.ErrorPageRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

/**
 * <p><b>自定义错误页面(4xx/5xx)处理类</b></p>
 * @Description 自定义错误页面(4xx/5xx)处理类</ p>
 * @author MengMeng
 * Created date: 2018/10/11 
 * @Since JDK 1.80_171
 */
@Component
public class ErrorConfiguration implements ErrorPageRegistrar {

   @Override
   public void registerErrorPages(ErrorPageRegistry registry) {
       ErrorPage[] errorPages = new ErrorPage[]{
    		   new ErrorPage(HttpStatus.FORBIDDEN, "/error/403"),
               new ErrorPage(HttpStatus.NOT_FOUND, "/error/404"),
               new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500"),
               new ErrorPage(Throwable.class, "/error/500")
       };
       registry.addErrorPages(errorPages);
   }
}

 

2、在controller包下创建ErrorController:

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
public class ErrorController {

	/**
	 *<p><b>自定义错误处理的Controller类</b></p>
	 * @Description 自定义错误处理的Controller类</ p>
	 * @author MengMeng
	 *<p>Created date: 2018/10/11</p>
	 * @version: 0.1
	 * @since JDK 1.80_144
	 */
	@RequestMapping(value = "/error/{code}")
	public String error(@PathVariable int code, Model model) {
		String pager = "";
		switch (code) {
		case 403:
			model.addAttribute("code", 403);
			pager = "error/page";
			break;
		case 404:
			model.addAttribute("code", 404);
			pager = "error/page";
			break;
		case 500:
			model.addAttribute("code", 500);
			pager = "error/page";
			break;
		}
		return pager;
	}

}

 

3、创建错误页面,HTML代码:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
	xmlns:th="http://www.thymeleaf.org">
<!--
    Title:错误处理页
    Description:错误处理页
    Author:MengMeng 
    Created date: 2018/10/11
    Version: Html5
 -->
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport"
	content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<title>网站访问失败</title>
<link rel="shortcut icon" th:href="@{/images/logo-black.png}"
	href="/images/logo.png" type="images/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="description" content="" />
<!-- 引入css -->
<th:block th:replace="pageFrame/basicsCss :: copy" />
</head>
<body class="hold-transition login-page">
	<div class="login-box">
		<!-- login-box-body -->

			<div id="doc_main">

				<section class="bd clearfix">
					<div class="module-error">
						<div class="error-main clearfix">
							<div class="info">
								<h2 class="title">你所访问的页面不存在!</h2>
								<div class="reason">
									<h3>可能的原因:</h3>
									<h3>1.内容填写有错误。</h3>
									<h3>2.链接过了保质期。</h3>
									<h3>3.所在页面时间过长。</h3>
									<h3>4.如果还未解决,请向管理员反馈!</h3>
								</div>
								<div class="oper">
									<h3>
										<a href="/login">回到网站首页&gt;</a>
									</h3>
								</div>
							</div>
						</div>
					</div>
				</section>
			</div>

			<!-- 引入js【body末尾引入js文件会使页面加载更快】 -->
			<th:block th:replace="pageFrame/basicsJs :: copy" />
			<!-- bootstrapValidator -->
			<script
				th:src="@{/plugins/bootstrapValidator/js/bootstrapValidator.min.js}"></script>
			<script th:src="@{/plugins/bootstrapValidator/js/language/zh_CN.js}"></script>
		</body>
</html>

 

效果图:

 

 

相关学习链接:

https://blog.csdn.net/hp_yangpeng/article/details/81004227

https://blog.csdn.net/fantomelarmes/article/details/81061435#%E8%87%AA%E5%AE%9A%E4%B9%89%E5%BC%82%E5%B8%B8%E9%A1%B5%E9%9D%A24xx5xx

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值