EL1008E:在“java.util.HashMap”类型的对象上找不到属性或字段“timestamp”——可能不是公共的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43751128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:41:32  来源:igfitidea点击:

EL1008E: Property or Field 'timestamp' cannot be found on object of type 'java.util.HashMap' - maybe not public?

javaspringspring-bootspring-el

提问by Chong Wang

When I use Spring Boot's global exception handler, I got this:

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

org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'timestamp' cannot be found on object of type 'java.util.HashMap' - maybe not public?

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

This is my code, and I have import the Spring Security and Thymeleaf in my project.

这是我的代码,我在我的项目中导入了 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
@GetMapping("/test")
public String test() throws Exception {
    throw new Exception("发生错误");
}
@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;
    }
}

回答by xierui

Your view name should be "error" but not "/error", the view resolver would find the template named error.html in the template folder, if the view resolver can not find one it will use the default one, in which the timestamp is needed in models.

你的视图名称应该是“error”而不是“/error”,视图解析器会在模板文件夹中找到名为error.html的模板,如果视图解析器找不到一个它会使用默认的,其中时间戳模型中需要。