我在百里香方面遇到麻烦,因此根据本文档thymeleaf,我只能使用片段渲染html页面的一部分,我尝试将其与控制器代码一起使用

@RequestMapping("/showContentPart")
public String showContentPart() {
...
return "index :: content";
}


和HTML

<div id="content">
  Only this will be rendered!!
</div>


但是,我想要的是用户单击导航栏上的链接,并且div应该呈现,并且布局应保持静态,换句话说。我想保持布局并更改div内容,但是当我单击我得到的链接只是获得没有布局的内容,我做错了什么?

最佳答案

您可以简单地将片段的名称作为模型属性传递,以替换布局中的内容:

@RequestMapping("/showContentPart")
public String showContentPart(Model model) {
    model.setAttribute("contentName", "content")
    return "layoutPage";
}


在布局页面中,您可以像这样包含您的内容:

<div id="layout">
    <div th:include="index :: ${contentName}"></div>
</div>


showContentPart方法将返回您的布局页面,但具有所需的内容。如果要包含其他内容,则只需执行类似的方法,但“ contentName”模型属性的值将不同。

10-07 19:00
查看更多