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')">
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')}">
For you to call your service methods from your Thymeleaf
template you need to add that service into your model like so
@Controller
public class PageController {
@Autowired
LicenseService licenseService;
@RequestMapping("/yourPage")
public String getYourPage(Model model) {
model.addAttribute("licenseService", licenseService);
return "yourPage.html";
}
}
After that you are good to use licenseService
in yourPage.html
.
<div th:if="${licenseService.verifyLicense() == true}">
<p> License verified </p>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With