布局:装饰在百里香中不起作用

时间:2018-01-20 11:25:17

标签: spring spring-boot thymeleaf

我正在尝试使用百万美元在我的应用程序中引入布局(作为here),但无法使其正常工作。我已经检查了this帖子。

的pom.xml

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.8.RELEASE</version>
        </parent> 
    ...
    <dependency>
        <groupId>nz.net.ultraq.thymeleaf</groupId>
        <artifactId>thymeleaf-layout-dialect</artifactId>
   </dependency>

这是我的MvcConfig

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
    ...
    @Bean
    public LayoutDialect layoutDialect() {
        return new LayoutDialect();
    }
}

layout.html给定here。没有变化。在检查某篇文章时添加了xmlns:th="http://www.thymeleaf.org

<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.thymeleaf.org">
<head>
  <title>Layout page</title>
</head>
<body>
  <header>
    <h1>My website</h1>
  </header>
  <div layout:fragment="content">
    <p>Page content goes here</p>
  </div>
  <footer>
    <p>My footer</p>
    <p layout:fragment="custom-footer">Custom footer here</p>
  </footer>  
</body>
</html>

我试图替换内容片段,如下面的login.html

所示
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.thymeleaf.org"
        layout:decorate="~{layout}">
    <head>
        <title>Application Login</title>
    </head>
    <body>
        <div layout:fragment="content">
            <div th:if="${param.error}">
                Invalid username and password.
            </div>
            <div th:if="${param.logout}">
                You have been logged out.
            </div>
            <form th:action="@{/login}" method="post">
                <div><label> User Name : <input type="text" name="username" value=""/> </label></div>
                <div><label> Password: <input type="password" name="password" value=""/> </label></div>
                <div><input type="submit" value="Sign In"/></div>
            </form>
         </div>   
    </body>
</html>

我错过了什么吗?

2 个答案:

答案 0 :(得分:2)

以下对我有用。

    <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>1.5.8.RELEASE</version>
   </parent>
   ...
   <!--<dependency>
            <groupId>nz.net.ultraq.thymeleaf</groupId>
            <artifactId>thymeleaf-layout-dialect</artifactId>
       </dependency>-->

    <properties>
        ...
        <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
        <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
    </properties>

答案 1 :(得分:0)

要与百里香一起使用并扩展视图,您必须(春季启动):

验证用于春季启动的依赖关系thymeleaft:

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-thymeleaf</artifactId> 
</dependency>

这是在百里香中认证的使用指令(例如:sec:authorize)

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>

这很重要,对于添加依赖方言

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

现在,我们需要在带有@Configuration注释的类中创建LayoutDialect bean。

@Bean
public LayoutDialect layoutDialect() {
    return new LayoutDialect();
}

最后一次:

mvn clean install

并运行服务器。