SpringBoot使用thymeleaf模版引擎配置自定义错误页面

1、要在Spring Boot项目中配置自定义的错误页面,你可以遵循以下步骤:

1.1、pom.xml引入thymeleaf

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

1.2、创建thymeleaf模版引擎的自定义错误页面

1、HTML 代码中的 是一个名字空间声明,用于启用 Thymeleaf 的属性。
2、Thymeleaf 是一个 Java 库,可以用于在网页上显示应用程序产生的数据或文本
3、在你的项目资源文件夹(通常是src/main/resources)中创建一个文件夹,命名为templates
4、在templates文件夹中创建你想要的错误页面,例如error.html、404.html或500.html

我的html放置在src/main/resources/templates/error下

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>访问错误</title>
    <style>
        body {
            background-color: #F2F2F2;
            font-family: Arial, sans-serif;
        }
        .error-container {
            text-align: center;
            margin-top: 50px;
        }
        .error-container h1 {
            font-size: 36px;
            color: #333;
        }
        .error-container p {
            font-size: 18px;
            color: #666;
        }
    </style>
</head>
<body>
    <div class="error-container">
        <h1>抱歉,页面未找到!</h1>
        <p>您访问的页面不存在或已被删除,请检查URL以及请求是否正确。</p>
		
    </div>
</body>
</html>

1.3、创建一个自定义错误处理类

创建一个自定义的错误处理类,实现ErrorController接口。该类将处理所有错误请求,并将请求重定向到你自定义的错误页面。

import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@RestController
public class CustomErrorController implements ErrorController {
	private static final String PATH = "/error";

	@RequestMapping(value = PATH, method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
	public ModelAndView handleError(HttpServletRequest request) {
		Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
		ModelAndView mav = new ModelAndView();
		if(statusCode == HttpStatus.NOT_FOUND.value()){
			//在src/main/resources/templates/error下寻找404.html页面
			mav.setViewName("error/404");
		}else if(statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()){
			//在src/main/resources/templates/error下寻找500.html页面
			mav.setViewName("error/500");
		}else if(statusCode == HttpStatus.FORBIDDEN.value()){
			//在src/main/resources/templates/error下寻找403.html页面
			mav.setViewName("error/403");
		}else{
			//默认错误页面
			mav.setViewName("error/error");
		}
		return mav;
	}

	@Override
	public String getErrorPath() {
		return PATH;
	}
}

1.4、配置错误页面

在application.properties或application.yml配置文件中,添加以下配置:

1.4.1、application.properties配置

server.error.whitelabel.enabled=false
spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false
##我的文件存放在/templates/error下
server.error.path=/error

1.4.2、application.yml配置

server:
  error:
    whitelabel:
      enabled: false
    path: /error

spring:
  mvc:
    throw-exception-if-no-handler-found: true
  resources:
    add-mappings: false
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值