Spring 和 thymeleaf 布局方言不起作用

Joh*_*lez 1 java spring spring-mvc thymeleaf

我是 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);

    }

}
Run Code Online (Sandbox Code Playgroud)

配置文件

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();
    }

}
Run Code Online (Sandbox Code Playgroud)

我的 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

index.html 文件

<p layout:decorator="layout" layout:fragment="content">
    asada
</p>
Run Code Online (Sandbox Code Playgroud)

问题是,当我打开 index.html 时,它不包含 layout.html 文件中的任何内容,文件在根目录中与其他文件相邻,因此那里没有文件夹,我是否错过了配置中的某些内容?谢谢

小智 5

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

<dependency>
    <groupId>nz.net.ultraq.thymeleaf</groupId>
    <artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

如果您使用的是 Spring Security,还可以使用以下依赖项:

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)