Thymeleaf(th:替换)不工作

时间:2016-11-01 13:40:06

标签: java spring thymeleaf

你好,我是Thymeleaf的新人,遇到了一个可能微不足道的问题,但百里香不会表现得像它应该的那样。只需一点帮助就会非常感激

我没有使用弹簧靴来学习。另外,我也是Spring的新手。可能会错过一两件事。

我有像这样的简单index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Index 2</title>
</head>
<body>
    <div th:replace="~{fragments/fragment1 :: fr1}"></div>
</body>
</html>

和fragment1.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
</head>
<body>
    <div th:fragment="fr1"><h1>HERE IS FRAGMENTS 1</h1></div>
</body>
</html>

据说它确实解析了模板,但结果根本没有改变。

这是我从浏览器页面源获取的内容

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Index 2</title>
</head>
<body>
    <div th:replace="~{fragments/fragment1 :: fr1}"></div>
</body>
</html>

并且它与原始index.html完全相同。

所以我认为它可能与配置有关,但它对我来说看起来不错。在我的另一个学习项目中,只需使用完全相同的配置即可正常工作。

这是配置

/* package and imports */

@Configuration
@EnableWebMvc
@ComponentScan("com.eshop")
public class WebConfig extends WebMvcConfigurerAdapter {

    private static final String UTF8 = "UTF-8";
    private static final String VIEWS = "/WEB-INF/templates/";

    private static final String RESOURCES_LOCATION = "/resources/";
    private static final String RESOURCES_HANDLER = RESOURCES_LOCATION + "**";


    //Thymeleaf Configuration
    @Bean
    public ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setPrefix(VIEWS);
        resolver.setSuffix(".html");
        resolver.setTemplateMode(TemplateMode.HTML);
        resolver.setCacheable(false);
        return resolver;
    }
    @Bean
    public SpringTemplateEngine templateEngine(){
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        templateEngine.setDialect(new SpringSecurityDialect());
        return templateEngine;
    }
    @Bean
    public ViewResolver viewResolver(){
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        viewResolver.setCharacterEncoding(UTF8);
        return viewResolver;
    }

    // tells DispatcherServlet to give static resources and not handle the resources itself
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }


    // handle various resources like javascript and css
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler( RESOURCES_HANDLER ).addResourceLocations( RESOURCES_LOCATION );
    }
}

的pom.xml

<!-- thymeleaf -->
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf</artifactId>
    <version>3.0.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring4</artifactId>
    <version>3.0.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity4</artifactId>
    <version>3.0.0.RELEASE</version>
</dependency>

这是项目树

webapp
|__resources
|__WEB-INF
   |__i18n
   |__templates
      |__fragments
         |__fragment1.html
      |__index.html

我在这里想念什么,如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

尝试调用th:不使用〜{}

替换
<div th:replace="fragments/fragment1 :: fr1"></div>

还要确保您有单独的html文件,名为fragment1.html

这个Layouts教程可以帮助你开始。