Search code examples
springthymeleaf

How to call a service method with Thymeleaf


As we can call service method in a jsp as follow(say to check authorization) :

<sec:authorize var="hasLicense" access="@licenseService.hasCapability('Event')"/>

How can we call this method when using Thymeleaf?

I know, We can check role as follow but couldn't get an example for above case:

<li class="link" sec:authorize="hasRole('event')">

Solution

  • Thymeleaf allows accessing beans registered at the Spring Application Context with the @beanName syntax, for example:

    <div th:text="${@urlService.getApplicationUrl()}">...</div>
    

    http://www.thymeleaf.org/doc/articles/springmvcaccessdata.html

    So this should work:

    <li class="link" sec:authorize="${@licenseService.hasCapability('Event')}">