Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring thymeleaf页眉和页脚动态包括_Java_Spring_Thymeleaf - Fatal编程技术网

Java Spring thymeleaf页眉和页脚动态包括

Java Spring thymeleaf页眉和页脚动态包括,java,spring,thymeleaf,Java,Spring,Thymeleaf,我最近在春天开始使用thymeleaf模板引擎。我想要实现的是——如果我的控制器是这样的话 @RequestMapping(value="/", method=RequestMethod.GET) public String index() { return "homePage"; } 然后我想在homePage.HTML中编写HTML代码,而不需要完整的HTML定义,如head、title、body等 <div>This is home page content</

我最近在春天开始使用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>

springmvc示例
这是主页内容

我更喜欢用头部分作为页眉片段,内容来自控制器,页脚部分来自页脚片段

总之,我如何才能做到这一点:

/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>
<div>This is home page content</div>
  </body>
 </html>

springmvc示例
/home.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>This is home page content</div>
  </body>
 </html>
这是主页内容
(此抛出错误)

/fragment/footer.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>This is home page content</div>
  </body>
 </html>

注意:我已经看到了这些示例


    • 我这样解决了这个问题:

      控制器指向index.html并给出“内容页”的属性

      这是我的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>
      
      
      //css包括etc标题
      
      片段/footer.html

      <div th:fragment="footer">
        //scripts includes
      </div>
      
      
      //脚本包括
      
      在我的文件夹视图中,我得到了所有包含内容的视图 视图/helloWorldView.html:

      <div th:fragment="helloWorldView">
          //Content
      </div>
      
      
      //内容
      
      我就是这样做到的

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

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

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

      /参考资料/templates/fragments/header.html

      <head th:fragment="header">
         //css includes etc title
      </head>
      
      
      
      /资源/模板/片段/footer.html

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

      步骤3:创建您的主页

      /参考资料/模板/home.html

      
      这是主页内容
      
      步骤4(Spring Boot 2):向pom.xml添加依赖项

      /pom.xml

      
      nz.net.ultraq.thymeleaf
      百里香方言
      
      您能粘贴包含页脚时出现的错误吗?您好@Joostje,谢谢您的回答,但我不想这样做。在索引中我只想要内容,在标题中我想要等等。你如何扩展它,使内容视图根据你点击的菜单项而改变?我必须提到这样的路径:使它适合我