内容丰富的胸腺布局 [英] Thymeleaf layout with multiple contents
问题描述
我是Thymeleaf模板引擎的新手,并且正在使用Spring Boot和Spring MVC开发应用程序.我正在使用application.properties
进行配置.
I'm new on Thymeleaf template engine, and I'm making an application with Spring Boot and Spring MVC. I'm working just with application.properties
for the configuration.
我想知道如何只写一个布局,但是写很多文件中的内容:例如content1.html
,content2.html
等,并使用已经具有标题的布局,页脚.
I want to know how I can write only ONE layout but the contents in many files: for example content1.html
, content2.html
, etc. and use the layout that already have the header, the footer.
如果可能,如何从控制器发送将在布局中替换的内容文件?
If it is possible, how can I send from the controller the content file that will be replaced in the layout?
推荐答案
您可以执行以下操作.假设您创建了一个页面,其中将嵌入所有其他内容-main.html
.看起来像这样:
You could do something like this. Let's say you create a page where all other content will be embedded - main.html
. It will look something like this:
<!doctype html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3" xmlns="http://www.w3.org/1999/xhtml">
<div th:fragment="mainPage(page, fragment)">
<h4>Some header</h4>
<div th:include="${page} :: ${fragment}"></div>
<h4>Some footer</h4>
</div>
</html>
然后您要创建一些页面,该页面将嵌入到main.html
页面-some-page.html
:
Then you want to create some page which will be embedded in your main.html
page - some-page.html
:
<!doctype html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3" xmlns="http://www.w3.org/1999/xhtml">
<div th:fragment="somePage">
<h1>${title}</h1>
</div>
</html>
目标是用some-page.html
中的内容替换main.html
中的<div th:include="${page} :: ${fragment}"></div>
.在控制器中,将如下所示:
The goal is to replace <div th:include="${page} :: ${fragment}"></div>
within main.html
with the content from some-page.html
. In controller, that will look like this:
@Controller
public class DemoController {
@RequestMapping
public String somePage(Model model) {
// Note that you can easy pass parameters to your "somePage" fragment
model.addAttribute("title", "Woa this works!");
return "main :: mainPage(page='some-page', fragment='somePage')";
}
}
然后您就去了!每次要交换main.html
中的内容时,只需在控制器的字符串中更改page
和fragment
参数.
And there you go! Every time when you want to swap content in main.html
, you just change page
and fragment
parameters within string in your controller.
这篇关于内容丰富的胸腺布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!