【问题标题】:SpringMVC with Bootstrap and Thymeleaf pages decoration带有 Bootstrap 和 Thymeleaf 页面装饰的 SpringMVC
【发布时间】: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


    【解决方案1】:

    嘿,我终于找到了解决方案:

    第一步

    我把页面和布局放在同一个文件夹里:

    src/main/webapp/WEB-INF/views/fragments/menubar.html
    src/main/webapp/WEB-INF/views/template.html
    src/main/webapp/WEB-INF/views/home.html (my page)
    

    第 2 步

    在“home.html”中我添加了“layout:decorate”

    <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="~{template}">
    ...
    

    在“template.html”中我改变了“layout:fragment”

    <div th:replace="fragments/menubar.html :: menubar"></div>
    
    <!-- Page Content -->
    <section layout:fragment="page_content">
        <p>Template content</p>
    </section>
    

    在“WebConfig.java”中,我删除了“viewResolver() 方法”并更改了其他方法,如下所示:

    @Configuration
    @ComponentScan(basePackages = {"packages"})
    @EnableWebMvc
    public class WebConfig {
        @Bean
        public SpringTemplateEngine templateEngine() {
            SpringTemplateEngine templateEngine = new SpringTemplateEngine();
            templateEngine.addDialect(new LayoutDialect()); // I added it again. Very important
            templateEngine.setTemplateResolver(thymeleafTemplateResolver());
            return templateEngine;
        }
    
        @Bean
        public SpringResourceTemplateResolver thymeleafTemplateResolver() {
            SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
            templateResolver.setTemplateMode("HTML");
            templateResolver.setPrefix("/WEB-INF/views/");
            templateResolver.setSuffix(".html");
            return templateResolver;
        }
    
        @Bean
        public ThymeleafViewResolver thymeleafViewResolver() {
            ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
            viewResolver.setTemplateEngine(templateEngine());
            return viewResolver;
        }
    }
    

    但是...问题仍然存在。我无法将我的页面和布局保存在不同的文件夹中。如果我使用以前的结构并输入: layout:decorate="~{layout/template}" 页面将显示为空白页面。

    您提出的任何解决方案和想法对我来说都是完美的。 虽然找到了部分解决方案。

    =========== 更新:===========

    找到了最后一部分的解决方案。我在“template.html”中有一个错误。 我应该把它写成: “布局/片段/menubar.html :: 菜单栏” 结构:

    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)
    

    代码将是:

    <!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="layout/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>
    

    我会把它作为一个解决方案,在系统允许的几个小时内。

    【讨论】:

    • 系统说我明天接受我的答案作为解决方案。我会等……
    猜你喜欢
    • 2020-02-18
    • 2014-12-04
    • 2023-04-01
    • 2021-01-11
    • 2021-09-19
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多