【问题标题】:hasRole() within th:remove is not workingth:remove 中的 hasRole() 不起作用
【发布时间】:2018-04-23 19:58:55
【问题描述】:

我想禁用特定链接但保留其文本,使其看起来像是被禁用。我正在尝试在锚标记内放置具有特定条件的 th:remove 。我在 ThymeLeaf 教程页面上找到了这个:

<a href="/something" th:remove="${condition}? tag">Link text not to be removed</a>

基于此,我正在尝试这样做:

<li>
    <a th:href="@{/config/mod/}"
       th:remove="${#authorization.expression('hasRole(''VIEW_MODULE_STATUS'')')}? tag">
        <i class="fa fa-gear"></i>&nbsp;[[#{webadmin.view.config.module.title.short}]]
    </a>
</li>

其中 VIEW_MODULE_STATUS 是角色。这种情况似乎不起作用,我不明白为什么。

仅供参考:我在锚标记中使用了sec:authorize="hasRole('VIEW_MODULE_STATUS')",它工作正常。我想避免这种方法,因为它完全删除了文本和链接。有没有其他方法可以禁用链接并使用 ThymeLeaf 保留文本?

(我使用的是百里香3.0)

【问题讨论】:

    标签: java spring-security thymeleaf spring-el


    【解决方案1】:

    表达式th:remove="${#authorization.expression('hasRole(''VIEW_MODULE_STATUS'')')}? tag"中使用的三元运算符有误

    三元运算符的形式为condition? 'true' : 'false'。所以你必须更新你的表达。然后你不能禁用&lt;a&gt; 标签,你唯一能做的就是将它的href 属性更新为#javascript:void(0);,这样它就没有任何动作了。你可以这样做:

    th:href="${#authorization.expression('hasRole(''VIEW_MODULE_STATUS'')') ? '/something' : '#'}"
    

    【讨论】:

    • 感谢您的快速回复。我试过了,它不工作。这是我的 th:ref:th:href="${#authorization.expression('hasAuthority(''VIEW_MODULE_STATUS'')') ? @{/config/mod/} : '#'}"
    • 您不能在${} 中使用@{}。您必须将其写为:@{${}}
    • 我的错。这也不起作用:th:href="@{${#authorization.expression('hasAuthority(''VIEW_MODULE_STATUS'')') ? {/config/mod/} : '#'}}"
    • 为什么{/config/mod}{} 中。只需将它们放在'' 之间,例如/config/mod/
    • 哇!那行得通。这是我的最终版本:th:href="@{${#authorization.expression('hasAuthority(''VIEW_MODULE_STATUS'')') ? '/config/mod/' : '#'}}" 不过有一个问题:当鼠标悬停在文本上时,鼠标看起来仍然像一只手,表明它是一个链接,但是当它点击时没有任何反应。这正常吗?
    猜你喜欢
    • 2015-08-27
    • 2017-06-16
    • 1970-01-01
    • 2020-06-24
    • 2015-12-09
    • 2015-06-06
    • 2015-05-10
    相关资源
    最近更新 更多