2

I want to create a fragment with thymeleaf that has its own controller, so anytime I include the fragment, the controller is called and fills the necessary model attributes. To me this sounds like a basic request but I am new to thymeleaf and can't figure it out. So for example I have a fragment like this:

<div th:fragment="table">
  <tr th:each="prod : ${prods}">
    <td th:text="${prod.name}"/>
  </tr>
</div>

In addition to this fragment, I would have a controller that looks somewhat like this:

@RequestMapping(value="/getProducts")
public Model products(Model model){
    List<String> products = getProductList();
    model.addAttribute("prods", products)
    return model;
}

So how can I bind those two? I am using spring-boot and I did not change or edit any resolver. Thanks, Peer

peer
  • 344
  • 1
  • 4
  • 17

3 Answers3

1

You can include another response into current page with

<th:block th:utext="${#servletContext.getRequestDispatcher('/path/to/fragment').include(#request,#response)}"/>

(method returns void, wrapped with utext to simply call it)

If this is frequent scenario, you can make it into a fragment accepting path parameter

<th:block th:fragment="include(url)">
    <th:block th:utext="${#servletContext.getRequestDispatcher(url).include(#request,#response)}"/>
</th:block>

and call with

<th:block th:include="~{::include(url='/path/to/fragment')}"/>
aimozg
  • 142
  • 1
  • 10
0

The idea von Spring MVC and a model is, that in the view only data a rendered. So it's a bad idea to call a service or a controller from any place in a template.

You can solve this problem with a function addDataForTableFragment(Model model). This must be called from your controller which uses the template with the fragment. If you need the data at many method have a look a "ModelAttribute".

Community
  • 1
  • 1
niels
  • 7,321
  • 2
  • 38
  • 58
-1

I don't have a reference handy but it sounds like you want the Thymeleaf element that makes a REST call and allows you to modify the DOM using the results. It's used to populate menus or lists, control visibility, etc. Each REST endpoint is a separate Controller.

It could make more substantial changes to the DOM but you'll lose the benefits of using a framework since everything is done on the server side.

This was covered in one of the online (Udemy?) courses on Spring MVC and Thymeleaf.

For anyone coming across this question in the future - you would do this since it allows the Controller to focus on doing just one thing. That means they're simpler and you don't have to worry about changing your Controller if the page is modified and needs to pull in additional data.

It's also faster since you don't have to wait for all of the data to be available (or for a timeout) before preparing your response. You can respond much more quickly - perhaps with no customized data at all - and have the custom data populate as it becomes available.

Finally there's no requirement that all of the data come from the same place. It's much easier to use microservices with specialization if each provides a REST Controller but doesn't make any assumptions about how the data will be used.

bgiles
  • 1,200
  • 11
  • 12