0

I would like to use my Java class constants with hasRole in my Thymeleaf template.

Today I use :

<div sec:authorize="${hasRole('USR')}">
...
</div>

But I would like to use my constants (declared inside my java class)

public class Consts{
  public static final String USR_CONST = "USER"; 
}

How can I change the string value ('USR' , hasRole parameter ) to constant USR_CONST ?

Anderson Rossi
  • 493
  • 8
  • 21

1 Answers1

1

You can use the SpEl Type Operator T to access static constants. eg:

<div sec:authorize="${hasRole(T(Consts).USR_CONST)}">
    ...
</div>

You'll need to specify the fully qualified path to your Consts class within the T(...)

Andrew
  • 1,269
  • 7
  • 9