Spring百里叶的页眉和页脚动态包括

时间:2016-04-16 10:38:57

标签: java spring thymeleaf

我最近在春天开始使用百里香模板引擎。我想要实现的是 - 如果我的控制器是这个

@RequestMapping(value="/", method=RequestMethod.GET)
public String index() {
    return "homePage";
}

然后我想在homePage.html中编写HTML代码而没有完整的HTML定义,如head,title,body等。

<div>This is home page content</div>

不要像这样在homePage.html中写。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" >
<head>
    <title>Spring MVC Example</title>
</head>
<body>
    <div th:include="fragments/header::head"></div>
    <div>This is home page content</div>
    <div th:include="fragments/footer::foot"></div>
</body>

我更倾向于从标题片段获取头部,控制器和页脚从页脚片段中获取内容。

总的来说 - 我如何才能实现这一目标:

/fragment/header.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" >
<head>
    <title>Spring MVC Example</title>
</head>
<body>

/home.html

<div>This is home page content</div>

(这个投掷错误)

/fragment/footer.html

  </body>
 </html>

注意:我已经看过这些例子

2 个答案:

答案 0 :(得分:14)

我这样解决了这个问题:

Controller指向index.html并提供&#34;内容页面&#34;

的属性
@RequestMapping(value={"/"})
   public String root(Locale locale, ModelMap model) {
   model.addAttribute("content", "helloWorldView");     
   return "index";
}

这是我的index.html:

<!DOCTYPE html>
<html lang="en">
<head lang="en" th:replace="fragments/header :: header"> </head>
  <body>
  <div class="container">

     <div th:replace="@{'views/' + ${content}} :: ${content}"></div>

  </div>
<div lang="en" th:replace="fragments/footer :: footer"> </div>
</body>
</html>

所以我有一个带页眉和页脚的文件夹片段:

片段/ header.html中

<head th:fragment="header">
   //css includes etc title
</head>

片段/ footer.html

<div th:fragment="footer">
  //scripts includes
</div>

在我的文件夹视图中,我获得了包含内容的所有视图 视图/ helloWorldView.html:

<div th:fragment="helloWorldView">
    //Content
</div>

答案 1 :(得分:14)

这就是我实现它的方式。

第1步:创建默认布局文件

  

资源/模板/布局/ default.html中

<!DOCTYPE html>
<html>
<head th:replace="fragments/header :: head"></head>
<body>
   <div class="container">
     <div layout:fragment="content"></div>
     <div th:replace="fragments/footer :: footer"></div>
   </div>
</body>
</html>

第2步:创建页眉和页脚片段。

  

/resources/templates/fragments/header.html

<head th:fragment="head">
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
  <meta content="ie=edge" http-equiv="x-ua-compatible" />
  <title th:text="${metaTitle} ? ${metaTitle} : 'default title'"></title>
  <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}"/>
  <link rel="stylesheet" href="/css/style.css"/>
</head>
  

/resources/templates/fragments/footer.html

<div class="footer text-center" th:fragment="footer">
  <p> <a href="#"> Terms of Use</a> | <a href="#">What's New</a> | <a href="#">Help</a> </p>
  <!-- javascripts -->
  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js">
</script>
</div>

第3步:创建主页

  

/resources/templates/home.html

<!DOCTYPE html>
<html lang="en"
  xmlns:th="http://www.thymeleaf.org"
  xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
  layout:decorator="layouts/default">
 <body>
  <div id="page" layout:fragment="content">
  <div>This is home page content</div>
  </div>
 </body>
</html>

步骤4(Spring Boot 2):向pom.xml添加依赖项

  

/pom.xml

<dependency>
    <groupId>nz.net.ultraq.thymeleaf</groupId>
    <artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>