当前位置: 首页 > 知识库问答 >
问题:

计算SpringEL表达式的Spring Boot异常

葛修永
2023-03-14

错误4904---[nio-8080-exec-1]org.THYMELEAF.templateEngine
:[THYMELEAF][http-nio-8080-exec-1]异常处理模板“index”:模板分析期间出错(模板:“class path resource[templates/index.html]”)

org.thymeleaf.exceptions.templateInputException:在org.thymeleaf.templateParser.markup.abstractmarkuptemplateParser.parse(abstractmarkuptemplateParser.java:241)~[thymeleaf-3.0.11.release.jar]在org.thymeleaf.templateParser.java:241)~[thymeleaf-3.0.11.release]在org.thymeleaf.engine.templateManager.parse(ymeleafview.render(thymeleafview.java:189)[thymeleaf-spring5-3.0.11.release.jar:3.0.11.release.render(dispatcherservlet.java:1370)[spring-webmvc-5.1.3.release.jar:5.1.3.release](dispatcherservlet.java:1370)[spring-webmvc-5.1.3.release.jar:5.1.3.release](dispatcherservlet.processdispatchresult(

原因:org.attoparser.ParseException:计算SpringEL表达式时出现异常:“#authorization.expression('is authenticated()')和#authorization.expression('has authority('user'')')”(模板:“fragments/navbar”-第8行,第15栏)

pom.xml

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        <version>3.0.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>

    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

当我试图检查权限时,问题就从这里来了

<html lang="en" xmlns="w3.org/1999/xhtml" xmlns:th="thymeleaf.org"> 
<th:block th:fragment="navbar">
    <th:block if:sec:authorize="isAnonymous()">
        <th:block th:replace="fragments/navbar-guest"></th:block>
    </th:block>
    <th:block th:if="${#authorization.expression('isAuthenticated()') and #authorization.expression('hasAuthority(''USER'')')}">
        <th:block th:replace="fragments/navbar-user"></th:block>
    </th:block>
    <th:block th:if="${#authorization.expression('isAuthenticated()') and #authorization.expression('hasAuthority(''ADMIN'')')}">
        <th:block th:replace="fragments/navbar-admin"></th:block>
    </th:block>
    <th:block th:if="${#authorization.expression('isAuthenticated()') and #authorization.expression('hasAuthority(''MODERATOR'')')}">
        <th:block th:replace="fragments/navbar-user"></th:block>
    </th:block>
</th:block>

共有1个答案

董嘉祯
2023-03-14

确保您在pom.xml中使用了thymeleaf-extras-springsecurity4

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

和xmlns:sec。

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
  xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
 类似资料:
  • 我想添加日期,但在保存计算SpringEL表达式的表单异常时出错:“#dates.format(Passation.DatePassation,'dd-mmm-yyyy')” 1-胸腺嘧啶: 2级通行 “谢谢你的帮助!

  • 使用DevTools控制台的求值计算功能,探测页面上任何项的状态。 DevTools控制台允许您以特别的方式了解页面中项的状态。他可以计算JavaScript的任何表达式,控制台本身支持几个功能。 TL;DR 计算键入的表达式。 使用其中一个快捷方式选择元素。 使用 inspect()检查DOM元素和JavaScript对象。 使用$0 - 4访问最近选择的元素和对象。 操作表达式 在控制台中输入

  • 主要内容:示例在MS Access中,表达式就像Excel中的公式。 这包括一些可以单独使用或组合使用以产生结果的元素。 表达式可以包括运算符,常量,函数和标识符。 表达式可用于执行各种任务,从检索控件的值或将查询条件提供给查询来创建计算的控件和字段,或者为报表定义组级别。 在本章中,我们将着重于创建一个称为计算表达式的非常具体的表达式。创建几个计算的字段,这些字段将计算并显示不存储在数据库本身任何位置的数据,

  • 当我们想临时修攺某个变量的值或查看其内部方法返回值的时候,可以使用计算表达式功能. Android Studio中提供了一个计算表达式和代码片段的功能, 使用起来非常方便.它除了支持正则表达式计算以外,还支持操作表达式,匿名表达式和内部类的计算. 两种计算模式: Expression Mode: 计算单行表达式 Code Fragment Mode: 计算代码片段, 我们可以对声明、赋值、循环和i

  • 我们正在使用Spring webflow ThymeLeaf并尝试在html页面中访问session.getAttribute()。 有点新的百里香叶,我知道百里香叶有2种方法来解决会议即。${session.something}和$ { # session . get attribute(' something ')}。 我们使用的代码类似于下面的代码,偶尔会失败。 在本地环境中,我从未见过故障

  • Evaluates simple math expression like 2*4 or 10/2 and outputs its result. You can use \ operator which is equivalent to round(a/b). 计算简单的数学表达式,比如2*4 或 10/2,并输出结果。\ 操作符结果同 round(a/b)。 Very useful in CS