Dan*_*ald 5 java spring thymeleaf
您好,我是 Thymeleaf 的新手,遇到了一个可能微不足道的问题,但 thymeleaf 的行为并不像它应该的那样。只是一点帮助将不胜感激
我不使用spring boot来学习。此外,我对 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>
Run Code Online (Sandbox Code Playgroud)
和 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>
Run Code Online (Sandbox Code Playgroud)
据说它确实解析了模板,但结果根本没有改变。
这是我从浏览器页面源中得到的
<!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>
Run Code Online (Sandbox Code Playgroud)
是的,它与原始 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 );
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
这是项目树
webapp
|__resources
|__WEB-INF
|__i18n
|__templates
|__fragments
|__fragment1.html
|__index.html
Run Code Online (Sandbox Code Playgroud)
我在这里想念什么,我该如何解决这个问题?
尝试在不带 ~{} 的情况下调用 th:replace
<div th:replace="fragments/fragment1 :: fr1"></div>
Run Code Online (Sandbox Code Playgroud)
还要确保您有一个名为fragment1.html的单独的html文件
这个布局教程应该可以帮助您入门。
| 归档时间: |
|
| 查看次数: |
5100 次 |
| 最近记录: |