【问题标题】:Thymeleaf render on fragmentsThymeleaf 在片段上渲染
【发布时间】:2017-09-07 00:19:23
【问题描述】:

我在使用百里香叶时遇到了麻烦,根据此文档thymeleaf 我只能使用片段呈现我的 html 页面的一部分,我尝试将它与控制器代码一起使用

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

在 HTML 中

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

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

【问题讨论】:

  • Thymeleaf 并不真正支持这种自动刷新。它是一种服务器端渲染技术。您将不得不编写自己的 javascript 来执行此操作,或者使用其他框架。

标签: java html spring thymeleaf


【解决方案1】:

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

@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”模型属性的值不同。

【讨论】:

  • 对不起,仍然无法正常工作,但是现在返回我的布局,div 中的片段仍然丢失...
猜你喜欢
  • 2016-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多