计算SpringEL表达式的异常

计算SpringEL表达式的异常,spring,exception,authorization,expression,thymeleaf,Spring,Exception,Authorization,Expression,Thymeleaf,我试图将thymeleaf与spring一起使用,但在解析以下header.html片段时遇到问题: <!DOCTYPE html> <html> <head> </head> <body> <div class="navbar navbar-inverse navbar-fixed-top" th:fragment="header"> <div class="container">

我试图将thymeleaf与spring一起使用,但在解析以下
header.html
片段时遇到问题:

<!DOCTYPE html>
<html>
<head>
</head>
  <body>
    <div class="navbar navbar-inverse navbar-fixed-top" th:fragment="header">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".nav-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">My project</a>
        </div>
        <div class="navbar-collapse collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#" th:href="@{/}">Home</a></li>
            <li><a href="#" th:href="@{/message}">Messages</a></li>
            <li><a href="#" th:href="@{/task}">Tasks</a></li>
          </ul>
          <ul class="nav navbar-nav navbar-right">
            <li th:if="${#authorization.expression('!isAuthenticated()')}">
              <a href="/signin" th:href="@{/signin}">Sign in</a>
            </li>
            <li th:if="${#authorization.expression('isAuthenticated()')}">
              <a href="/logout" th:href="@{/logout}">Logout</a>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </body>
</html>

怎么了?我是不是在thymeleaf的配置中遗漏了什么?

正确的否定thymeleaf表达式的方法就是在它前面加上单词not:

 th:if="not ${#authorization.expression('isAuthenticated()')}"
或者作为另一个例子:

th:if="${not #lists.isEmpty(pager)}"

我相信这就是您想要的。

问题在于我没有将SpringSecurity方言包括到模板引擎中:

<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
      ...
      <property name="additionalDialects">
        <set>
          <!-- Note the package would change to 'springsecurity3' if you are using that version -->
          <bean class="org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect"/>
        </set>
      </property>
      ...
</bean>

...
...
纠正此错误后,一切正常。

使用注释:

    @Configuration
    @EnableWebMvc
    public class WebMvcConfig extends WebMvcConfigurerAdapter {
    ...
        @Bean
        public SpringTemplateEngine templateEngine() {
            SpringTemplateEngine templateEngine = new SpringTemplateEngine();
            ...
            templateEngine.addDialect(new SpringSecurityDialect());
        return templateEngine;
}
由于“org.springframework.expression.spel.SpelEvaluationException:EL1007E:在null上找不到属性或字段“userPrincipal”,我也收到了此错误

我在pom.xml文件中添加了以下内容,错误得到了解决。 1.依赖性

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

    </dependency>

org.thymeleaf.extras
thymeleaf-extras-springsecurity5
3.0.4.1发布
二,。在标签中添加以下内容:

<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
3.0.2.0版本
2.1.1
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>