How to call a service method with Thymeleaf
How can we call the service method in jsp as follows (say for authorization check):
<sec:authorize var="hasLicense" access="@licenseService.hasCapability('Event')"/>
How can we call this method when using Thymeleaf?
I know we can check the role as follow, but couldn't get an example for the above case:
<li class="link" sec:authorize="hasRole('event')">
+3
source to share
2 answers
Thymeleaf allows access to beans registered in a Spring Application Context with @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')}">
+8
source to share
In order for you to call your service methods from your template Thymeleaf
, you need to add this service to your model as follows
@Controller
public class PageController {
@Autowired
LicenseService licenseService;
@RequestMapping("/yourPage")
public String getYourPage(Model model) {
model.add("licenseService", licenseService);
return "yourPage.html";
}
}
Then you can use licenseService
in yourPage.html
.
<div th:if="${licenseService.verifyLicense() == true}">
<p>
License verified
</p>
</div>
0
source to share