Java 在片段上渲染
我在使用thymeleaf时遇到了一个麻烦,因此根据这个文档,我可以使用一个片段来呈现html页面的一部分,我尝试将它与控制器代码一起使用Java 在片段上渲染,java,html,spring,thymeleaf,Java,Html,Spring,Thymeleaf,我在使用thymeleaf时遇到了一个麻烦,因此根据这个文档,我可以使用一个片段来呈现html页面的一部分,我尝试将它与控制器代码一起使用 @RequestMapping("/showContentPart") public String showContentPart() { ... return "index :: content"; } 和HTML格式 <div id="content"> Only this will be rendered!! </div>
@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”model属性的值不同。抱歉,仍然不起作用,但是现在返回我的布局,div中的片段仍然丢失……Thymeleaf不支持这种自动刷新。这是一种服务器端渲染技术。为此,您必须编写自己的javascript,或者使用其他框架。