在 Thymeleaf
View 中,仅当当前用户属于某个角色时,我才想向标记添加属性。
为具有 ROLE_USER
的用户生成的 html 应该是:
<paper-input readonly label="Mobile" value="[[info.user.gsm]]"></paper-input>
对于拥有 ROLE_ADMIN
的用户,它应该是:
<paper-input label="Mobile" value="[[info.user.gsm]]"></paper-input>
这怎么可能做到?
最佳答案
由于 Thymeleaf 对 boolean 属性的特殊处理,它非常简单:
<paper-input th:readonly="${#request.isUserInRole('USER')}" label="Mobile" value="[[info.user.gsm]]"></paper-input>
来自docs关于固定值 boolean 属性,其中 readonly
是其中之一:
The Standard Dialect includes attributes that allow you to set these attributes by evaluating a condition, so that if evaluated to true, the attribute will be set to its fixed value, and if evaluated to false, the attribute will not be set
关于java - 仅为特定用户向标签添加属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46557008/