Thymeleaf标签 tb:remove

示例:SpringBoot仅错误页面源代码而不在页面中显示错误信息给开发测试人员

第一步:编写自定义统一处理异常的控制器代码:

@Log4j2
@ControllerAdvice
public class ExceptionHandlerController {
    @ExceptionHandler(Exception.class)
    public ModelAndView exceptionHandler(HttpServletRequest request, Exception e) {
        //日志记录异常
        log.info("Request URL: {}, Exception: {}", request.getRequestURL(), e);
        ModelAndView mav = new ModelAndView(); //此处不能将这个参数直接定义在方法参数中
        mav.addObject("url", request.getRequestURL());
        mav.addObject("exception", e);
        mav.setViewName("error/error");
        return mav;
    }
}

第二步:在resources/error目录下编写错误页面error.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <h1>error</h1>
        <div>
            <div th:utext="'<!--'" th:remove="tag"></div>
            <div th:utext="'Failed Request URL: '+${url}" th:remove="tag"></div>
            <div th:utext="'Exception message: '+${exception.message}" th:remove="tag"></div>
            <ul th:remove="tag">
                <li th:each="st: ${exception.stackTrace}" th:remove="tag">
                    <span th:utext="${st}" th:remove="tag"></span>
                </li>
            </ul>
            <div th:utext="'-->'" th:remove="tag"></div>
        </div>
    </body>
</html>

注:

  • th:utext:对标签进行转义
  • tb:remove:标签内容不在页面中显示,只在页面源代码中显示

你可能感兴趣的:(#,SpringBoot,服务器信息只在网页源代码中显示)