【问题标题】:Springboot/Thyme leaf exceptionSpring Boot/Thymeleaf 异常
【发布时间】: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>
         &nbsp;|&nbsp;
      <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


    【解决方案1】:

    再次检查该属性。 property="principal.username"。原则有属性username 参考:See this tutorial for reference

    【讨论】:

      【解决方案2】:

      似乎request.userPrincipal.name 为空。仔细检查请求对象。你在哪里创建它?

      【讨论】:

        猜你喜欢
        • 2021-09-03
        • 1970-01-01
        • 2019-04-02
        • 1970-01-01
        • 2017-05-26
        • 2019-06-28
        • 2019-10-11
        • 2014-11-02
        • 2019-01-06
        相关资源
        最近更新 更多