Reputation: 1
I'm having a troublesome with thymeleaf, accordingly to this documentation thymeleaf I'm able to render just a part of my html page, using a fragment, I tried to use it with the controller code
@RequestMapping("/showContentPart")
public String showContentPart() {
...
return "index :: content";
}
and in HTML
<div id="content">
Only this will be rendered!!
</div>
However what I want is that a user click on a link on a nav bar and the div should render and the layout should stay static, in other words.. I want to maintain my layout and change the div content, however when I click for the link I get just get the content without my layout, I'm doing somehting wrong?
Upvotes: 0
Views: 1489
Reputation: 14806
You can simply pass the name of your fragment as a model attribute to replace the content in your layout:
@RequestMapping("/showContentPart")
public String showContentPart(Model model) {
model.setAttribute("contentName", "content")
return "layoutPage";
}
In layout page, you can include your content like this:
<div id="layout">
<div th:include="index :: ${contentName}"></div>
</div>
showContentPart
method will return your layout page, but with desired content. If you want to include some other content, you just make the similar method but with different value of "contentName" model attribute.
Upvotes: 1