【问题标题】:Spring and thymeleaf layout dialect not working春天和百里香布局方言不起作用
【发布时间】:2015-01-23 00:13:00
【问题描述】:

我是 Spring 和 thymeleaf 的新手,我正在使用 JSF + Facelets,所以我选择 thymeleaf 布局方言的方法,因为它与 Facelets 非常相似,但是,由于某种原因,它不适用于我的简单项目。

我的配置文件中有这个

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

public class AppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext sc) throws ServletException {

        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
        applicationContext.register(WebConfig.class);

    }

}

WebConfig.java

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


@Configuration
@EnableWebMvc
@Import(ThymeLeafConfig.class)
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}

我的 TyhmeLeafConfig

import nz.net.ultraq.thymeleaf.LayoutDialect;
import org.springframework.context.annotation.Bean;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;


public class ThymeLeafConfig {

    @Bean
    public ServletContextTemplateResolver templateResolver() {
        ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
        resolver.setPrefix("/WEB-INF/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode("HTML5");
        resolver.setOrder(1);
        resolver.setCacheable(false);
        return resolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        templateEngine.setDialect(new LayoutDialect());
        return templateEngine;
    }

    @Bean
    public ThymeleafViewResolver viewResolver() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        viewResolver.setCharacterEncoding("UTF-8");
        return viewResolver;
    }
}

layout.html 文件

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
  <head>
    <title>Layout page</title>
    <script src="js/jquery.js"></script>
  </head>
  <body>
    <header>
      <h1>MASTER!!!!</h1>
    </header>
    <section layout:fragment="content">
      <p>MASTER CODE</p>
    </section>
    <footer>
      <p>My footer</p>
      <p layout:fragment="custom-footer">Custom footer here</p>
    </footer>  
  </body>
</html>

index.html 文件

<p layout:decorator="layout" layout:fragment="content">
    asada
</p>

问题是,当我打开 index.html 时,它不包含 layout.html 文件中的任何内容,文件位于根目录中的其他文件旁边,因此其中没有文件夹,我是否错过了配置?谢谢

【问题讨论】:

标签: java spring spring-mvc thymeleaf


【解决方案1】:

即使使用 Spring Boot,您也必须像这样在依赖项中包含布局方言:

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

如果您使用 Spring Security,也请使用以下依赖项:

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

【讨论】:

    【解决方案2】:

    根据https://github.com/ultraq/thymeleaf-layout-dialect 中的Read.md,您需要指定文件,而不是文件名。所以你应该有(如果它们在同一个目录中):

    <p layout:decorator="layout.html" layout:fragment="content">
        asada
    </p>
    

    此外,Thymeleaf 支持的布局对于将框架包含到代码中也非常有用。更多信息可以在这里找到:http://www.thymeleaf.org/doc/articles/layouts.html

    【讨论】:

      【解决方案3】:

      https://github.com/ultraq/thymeleaf-layout-dialect/blob/main/CHANGELOG.md

      版本 3.0.0:不推荐使用的 layout:decorator 处理器已被删除 ->布局:装饰

      【讨论】:

      • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
      猜你喜欢
      • 2015-12-09
      • 1970-01-01
      • 2014-06-04
      • 1970-01-01
      • 1970-01-01
      • 2018-11-27
      • 2018-05-23
      • 2017-10-18
      • 1970-01-01
      相关资源
      最近更新 更多