无法让百万美元布局方言示例工作

时间:2017-12-27 19:35:21

标签: java spring-boot thymeleaf

我有一个springboot 1.5.9项目,我试图让百万美元的布局方言模板工作。

我按照这里给出的例子

https://ultraq.github.io/thymeleaf-layout-dialect/Examples.html

我使用以下代码

创建了一个Layout.html
<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
    <title>Layout page</title>
    <script src="common-script.js"></script>
</head>
<body>
<header>
    <h1>My website</h1>
</header>
<section layout:fragment="content">
    <p>Page content goes here</p>
</section>
<footer>
    <p>My footer</p>
    <p layout:fragment="custom-footer">Custom footer here</p>
</footer>
</body>
</html>

以及带有以下内容的content2.html

<p layout:decorate="{Layout}" layout:fragment="custom-footer">
    My Custom Footer
</p>

我的控制器看起来像

@RequestMapping(path = "/")
public String intialiseService(Model model) {
    return "content2";
}

当我访问root时,我得到的页面只返回了content2.html中的html

<p layout:decorate="{Layout}">
    My Custom Footer
</p>

我希望它返回Layout.html,并将页脚修改为content2.html内容

感激地接受任何建议。

UPDATE  我忘了添加我的依赖项:

dependencies {
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile "io.springfox:springfox-swagger2:2.6.1" //rest api description
    compile 'io.springfox:springfox-swagger-ui:2.6.1' //rest api description ui
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('info.cukes:cucumber-java:1.2.5') //to use cucumber
    testCompile('info.cukes:cucumber-junit:1.2.5') //to run in junit via the ide
    testCompile('info.cukes:cucumber-spring:1.2.5') //to use dependency injection
    acceptanceTestCompile('org.springframework:spring-tx:4.3.6.RELEASE') //to run acceptance tests from feature file in IDE
    compile "org.scala-lang:scala-library:${SCALA_VERSION}"
    loadTestCompile "io.gatling:gatling-http:${GATLING_VERSION}"
    loadTestCompile "io.gatling:gatling-core:${GATLING_VERSION}"
    loadTestCompile "io.gatling.highcharts:gatling-charts-highcharts:${GATLING_VERSION}"
    testCompile "io.gatling:gatling-app:${GATLING_VERSION}"
    compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: '2.9.2'
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.springframework.boot:spring-boot-devtools")
    compile group: 'nz.net.ultraq.thymeleaf', name: 'thymeleaf-layout-dialect', version: '1.1.3'
}

第二次更新:

这是有效的

https://progressive-code.com/post/14/Thymeleaf-Layout-Dialect-as-Decorator-Pattern-for-a-Spring-Boot-web-application

看起来有一些语法变化,所以我使用的文档把我带到了错误的道路上。

1 个答案:

答案 0 :(得分:0)

替换content2.html中的以下代码: <p layout:decorate="{Layout}" layout:fragment="custom-footer"> My Custom Footer </p>

用这个:

<p layout:decorate="~{Layout}" layout:fragment="custom-footer"> My Custom Footer </p>

我猜你在定义的装饰器前面缺少~