春天和百里香的布局方言不起作用

时间:2015-01-23 00:13:01

标签: java spring spring-mvc thymeleaf

我是Spring和thymeleaf的新手,我正在使用JSF + Facelets这样的方法我选择了百日咳布局方言,因为它与Facelets非常相似,但是,由于某些原因它在我的简单项目中不起作用。 / p>

我在配置文件中有这个

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文件中的任何内容,文件在root中的其他文件旁边,因此没有文件夹在那里,我是否遗漏了组态?感谢

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

即使使用Spring Boot,您也必须像这样在依赖项中包括layout-dialect:

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