【问题标题】:How do I use multiple conditions in thymleaf - "If statement"如何在百里香中使用多个条件 - “If 语句”
【发布时间】:2021-06-17 13:19:50
【问题描述】:

我试图仅为发表评论的人和管理员显示删除按钮,但是 当我尝试使用 if 语句或 sec:authorize 隐藏它时,解析模板时出错。 我尝试分别使用每个 if 条件执行两个单独的按钮,并且效果很好,但是当我使用 OR 组合它们时出现错误。

这是我到目前为止所写的。 // HTML

            <a th:if="${c.email}==${namePrincipal} or hasRole('ADMIN')" href="#" class="btn btn-secondary" style="background-color: #8db8e0; margin-left: 200px;">Delete</a>
       

错误信息: org.thymeleaf.exceptions.TemplateInputException:模板解析时出错(模板:“类路径资源[templates/show-cmets.html]”)

原因:org.attoparser.ParseException:无法解析为表达式:“${c.email}==${namePrincipal} 或 hasRole('ADMIN')”(模板:“show-cmets” - 第 34 行,第 20 列)

【问题讨论】:

  • 嘿,我想我正在将 thymeleaf 安全性与正常的 thymeleaf 语法​​混合在一起。 hasRole('ADMIN') 确实适用于 sec:authorize,但不适用于 th:if。我最后实现的是在控制器中创建一个“hasRoleAdmin”属性并像这样使用它:th:if="${hasRoleAdmin} 或 ${c.email}==${namePrincipal}"。感谢您的帮助

标签: if-statement thymeleaf or-operator


【解决方案1】:

根据角色授予权限,使用sec:authorize

       <div sec:authorize="hasRole('ROLE_ADMIN')">
                    <div th:if="${#authentication.principal.username} eq ${c.getEmail()}">


            <a href="#" class="btn btn-secondary" style="background-color: #8db8e0; margin-left: 200px;">Delete</a>

                    </div>
                </div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-16
    • 2014-01-07
    • 2018-02-03
    • 1970-01-01
    • 2018-02-27
    • 2017-04-05
    • 2011-09-12
    • 2013-12-17
    相关资源
    最近更新 更多