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

版权声明:如需转载,请备注链接: https://blog.csdn.net/W_Meng_H/article/details/83010969

此方法适用于项目打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

猜你喜欢

转载自blog.csdn.net/W_Meng_H/article/details/83010969