【问题标题】:layout:decorate is not working in thymeleaf布局:装饰在百里香中不起作用
【发布时间】:2018-01-20 11:25:17
【问题描述】:

我正在尝试使用 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>

我错过了什么吗?

【问题讨论】:

    标签: spring spring-boot thymeleaf


    【解决方案1】:

    以下内容对我有用。

        <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>
    

    【讨论】:

      【解决方案2】:

      要使用 thymeleaf 并扩展视图,您必须(春季启动):

      验证spring boot的依赖thymeleaf:

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

      这是用于在 thymeleaf 中验证的使用指令(例如: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
      

      并运行服务器。

      【讨论】:

      • 谢谢你。添加这个依赖 thymeleaf-layout-dialect 解决了我的问题
      猜你喜欢
      • 1970-01-01
      • 2019-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-12
      • 2021-01-09
      • 2020-04-10
      • 1970-01-01
      相关资源
      最近更新 更多