java Spring和百里香叶布局方言不工作

cygmwpex  于 2023-02-20  发布在  Java
关注(0)|答案(4)|浏览(136)

我是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文件中的任何内容,文件在根目录中是相邻的,因此没有文件夹,我在配置中遗漏了什么吗?谢谢

4xy9mtcn

4xy9mtcn1#

即使使用Sping 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>
3htmauhk

3htmauhk2#

版本3.0.0:已弃用 * 布局:装饰器 * 处理器已删除-〉* 布局:装饰 *(https://github.com/ultraq/thymeleaf-layout-dialect/blob/main/CHANGELOG.md

gg58donl

gg58donl3#

根据Read.mdhttps://github.com/ultraq/thymeleaf-layout-dialect中的www.example.com,您需要指定文件,而不是文件名。因此,您应该具有(如果它们位于同一目录中):

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

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

nfeuvbwi

nfeuvbwi4#

我在替换templateEngine.setDialect(new LayoutDialect())时遇到了类似的问题;
添加方言(新的布局方言());
这就解决了

相关问题