org.thymeleaf.exceptions.TemplateProcessingException:评估SpringEL表达式的异常:“#request.userPrincipal.name”

时间:2019-04-17 08:47:45

标签: html

每当我尝试运行Web应用程序时,都会出现以下错误。
你能帮忙吗?

2019-04-17 07:33:13.881 ERROR 21660 --- [io-9393-exec-10] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-9393-exec-10] Exception processing template "index": Exception evaluating SpringEL expression: "#request.userPrincipal.name" (template: "/_header" - line 10, col 48)
    org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#request.userPrincipal.name" (template: "/_header" - line 10, col 48)

 Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'name' cannot be found on null

我的header.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>

2 个答案:

答案 0 :(得分:1)

尝试像这样修改代码

<div class="header-container"
  xmlns:th="http://www.thymeleaf.org"
  xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity">

并更新porn.xml

        <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity5</artifactId>

    </dependency>

它将正常工作

答案 1 :(得分:0)

这看起来好像代码(isAuthenticated)没有生效。

尝试更新pom文件以使用以下版本: thymeleaf-extras-springsecurity5

并将您的html头文件更新为: xmlns:sec =“ http://www.thymeleaf.org/extras/spring-security”