【发布时间】:2019-09-05 10:35:41
【问题描述】:
我是 Springboot 的初学者,所以决定在购物车上工作。似乎找不到 org.springframework.expression.spel.SpelEvaluationException 的根:EL1007E:在 null 上找不到属性或字段“名称”。嵌套异常是 org.thymeleaf.exceptions.TemplateProcessingException:评估 SpringEL 表达式的异常:“#request.userPrincipal.name”(模板:“/_header”-第 11 行,第 48 列)] 根本原因
我已经尝试检查有问题的对象是否为空
if (userDetails != null){
model.addAttribute("userDetails", userDetails);
}
用户类是抽象的,顺便说一下“import org.springframework.security.core.userdetails.UserDetails;”
特定的 HTML 代码:
<div class="header-bar">
<th:block sec:authorize="isAuthenticated()">
Hello
<a th:href="@{/admin/accountInfo}"
th:utext="${#request.userPrincipal.name}">..</a>
|
<a th:href="@{/admin/logout}">Logout</a>
</th:block>
<th:block sec:authorize="!isAuthenticated()">
<a th:href="@{/admin/login}">Login</a>
</th:block>
</div>
还有我的控制器代码:
@RequestMapping(value = { "/admin/accountInfo" }, method = RequestMethod.GET)
public String accountInfo(Model model) {
UserDetails userDetails = (UserDetails)
SecurityContextHolder.getContext().getAuthentication().getPrincipal();
System.out.println(userDetails.getPassword());
System.out.println(userDetails.getUsername());
System.out.println(userDetails.isEnabled());
model.addAttribute("userDetails", userDetails);
return "accountInfo";
}
【问题讨论】:
标签: java spring-boot thymeleaf