org.thymeleaf.exceptions.Исключение TemplateProcessingException: исключение, оценивающее выражение SpringEL: «#request.userPrincipal.name «

#html

#HTML

Вопрос:

Всякий раз, когда я пытаюсь запустить свое веб-приложение, я получаю сообщение об ошибке, приведенное ниже.
Не могли бы вы помочь.

 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>
                amp;nbsp;|amp;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>
  

Ответ №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>
  

Он будет работать правильно

Ответ №2:

Похоже, что код (прошедший проверку подлинности) не вступает в силу.

Попробуйте обновить свой pom-файл, чтобы использовать следующую версию: thymeleaf-extras-springsecurity5

И обновите свой заголовочный файл html до: xmlns:sec=»http://www.thymeleaf.org/extras/spring-security «