EL1008E:在'java.util.HashMap'类型的对象上找不到属性或字段'timestamp' - 可能不公开?

Cho*_*ang 10 java spring spring-el spring-boot

当我使用Spring Boot的全局异常处理程序时,我得到了:

org.springframework.expression.spel.SpelEvaluationException:EL1008E:在'java.util.HashMap'类型的对象上找不到属性或字段'timestamp' - 可能不公开?

这是我的代码,我在我的项目中导入了Spring Security和Thymeleaf.

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
    <meta charset="UTF-8" />
    <title>??????</title>
</head>
<body>
<h1> ???? </h1>
<div th:text="${url ?: '????URL'}"></div>
<div th:text="${exception == null ? '??????' : exception.message}"></div>
</body>
</html
Run Code Online (Sandbox Code Playgroud)
@GetMapping("/test")
public String test() throws Exception {
    throw new Exception("????");
}
Run Code Online (Sandbox Code Playgroud)
@ControllerAdvice
public class GlobalExceptionHandler {

    private static final String DEFAULT_ERROR_VIEW = "/error";
    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest request, Exception e) {
        LOGGER.error("????", e);
        ModelAndView mav = new ModelAndView();
        mav.addObject("exception", e);
        mav.addObject("url", request.getRequestURI());
        mav.setViewName(DEFAULT_ERROR_VIEW);
        return mav;
    }
}
Run Code Online (Sandbox Code Playgroud)

xie*_*rui 5

您的视图名称应该是"错误"但不是"/ error",视图解析器会在模板文件夹中找到名为error.html的模板,如果视图解析器找不到它将使用默认值,其中时间戳在模型中需要.