Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf include template with own controller

I am trying to include the same bit of html into various pages. This template is a table that displays data from the database. I know Thymeleaf has fragments, but as far as I can tell I can't include a fragment with its own controller, which my template will need so it can retrieve the necessary data.

I have seen this question, but this template can be called multiple times on the same page and adding that many attributes to the Model does not seem like the correct solution(not to mention that the attribute names need to be unique).

Is there any way to include this template in Thymeleaf so it has its own controller separate from the page it is included too.

like image 532
Vinc Avatar asked Jul 06 '26 11:07

Vinc


1 Answers

I use an approach which I call "Service based rendering" :)

The idea behind is to leverage on the spring beans integration via @someService.

Instead of filling the Model you can call any bean with parameters coming from either the model, requestmapping, the template or fragment parameters or from wherever you can get them.

Little example:

<div th:each="someVar : ${@someService.getData(someParameter)}" th:text="${someVar}">Template Text</div>
like image 101
Martin Frey Avatar answered Jul 09 '26 07:07

Martin Frey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!