I want to disable a particular link but retain its text, giving it the appearance of being disabled. I am trying to place a th:remove with a certain condition inside the anchor tag. I found this on the ThymeLeaf tutorial page:
<a href="/something" th:remove="${condition}? tag">Link text not to be removed</a>
Based on that I'm trying to do this:
<li>
<a th:href="@{/config/mod/}"
th:remove="${#authorization.expression('hasRole(''VIEW_MODULE_STATUS'')')}? tag">
<i class="fa fa-gear"></i> [[#{webadmin.view.config.module.title.short}]]
</a>
</li>
where VIEW_MODULE_STATUS is the role. The condition does not seem to work and I can't understand why.
FYI: I have used sec:authorize="hasRole('VIEW_MODULE_STATUS')"
in the anchor tag and it works fine. I want to avoid this approach because it completely removed the text and the link. Is there any other approach to have the link disabled and retain the text using ThymeLeaf?
(I am using ThymeLeaf 3.0)
There is a mistake in the ternary operator being used in the expressions th:remove="${#authorization.expression('hasRole(''VIEW_MODULE_STATUS'')')}? tag"
A ternary operator is of form condition? 'true' : 'false'
. So you have to update your expression. Then you cannot disable an <a>
tag, the only thing you can do is update its href
attribute to be #
or javascript:void(0);
so that it doesn't have any action. You could do it as shown below:
th:href="${#authorization.expression('hasRole(''VIEW_MODULE_STATUS'')') ? '/something' : '#'}"