【发布时间】:2021-10-22 18:41:26
【问题描述】:
我在以下领域工作: “SpringMVC 5”,带有“Twitter Bootstrap 4”html 页面和“Thymeleaf 3”模板,在 IntelliJ EAP(最新版本)和 Tomcat9、Maven 中
我的项目结构:
src/main/java (WebConfig.java/Controlers/Beans)
src/main/resources (*.properties)
src/main/webapp/WEB-INF/views/layout/template.html
src/main/webapp/WEB-INF/views/fragments/menubar.html
src/main/webapp/WEB-INF/views/home.html (my page)
我正在使用这些教程: https://www.baeldung.com/spring-thymeleaf-fragments https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#template-layout
我有我的页面 (home.html)。我有我的模板(template.html)。 根据第二个教程:
- 我将“菜单栏”插入到“模板”中(此插入必须有效,因为我将菜单栏直接插入到“home.html”中,并且插入成功)
- 我无法从他们所说的第二个教程中解决的问题: 我如何根据“template.html”“装饰”我的“home.html”。我的意思是,我如何使用模板来装饰我的所有页面? 我在“home.html”中使用了以下内容,但它不起作用:
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{layout/template}"
我的文件是:
“home.html”
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<!-- These in the html-tag are not working
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{layout/template}"
-->
<body>
<th:block th:fragment="page_content"> <!-- IntelliJ says I cannot recognise the th:block -->
<h2>Home page</h2>
<p>Hello home page</p>
</th:block>
</body></html>
“menubar.html”
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light" th:fragment="menubar">
<div class="container-fluid">
<!-- bla-bla -->
</div>
</nav>
</body></html>
“模板.html”
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<!-- Stylesheets (Bootstrap and mine) here -->
<title>Template Title</title>
</head>
<body>
<div th:replace="fragments/menubar.html :: menubar"></div>
<!-- Page Content -->
<section class="page_content">
<div layout:fragment="page_content"></div>
</section>
<!-- Javascript scripts (Bootstrap and mine) here -->
</body></html>
“WebConfig.java”
@Configuration
@ComponentScan(basePackages = {"packages"})
@EnableWebMvc
public class WebConfig {
/*
I have a seperate controler that GETs my "home.html" successfuly
*/
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".html");
viewResolver.setExposeContextBeansAsAttributes(true);
return viewResolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
//templateEngine.addDialect(new LayoutDialect()); // This is not working at all
templateEngine.setTemplateResolver(thymeleafTemplateResolver());
return templateEngine;
}
@Bean
public SpringResourceTemplateResolver thymeleafTemplateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setTemplateMode("HTML5");
templateResolver.setPrefix("/WEB-INF/views/layout/");
templateResolver.setSuffix(".html");
return templateResolver;
}
@Bean
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
return viewResolver;
}
}
在我的 pom 中(其中一些):
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.12.RELEASE</version>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-spring-environment-dialect</artifactId>
<version>1.0.1</version>
</dependency>
请人帮忙。提前致谢
【问题讨论】:
标签: java html twitter-bootstrap spring-mvc thymeleaf