当前位置: 首页 > 知识库问答 >
问题:

Spring Boot和Thymeleaf:找不到超文本标记语言模板

史鸿运
2023-03-14

我正试图用Thymeleaf创建一个基于Spring Boot的应用程序。我使用PetClinic样本作为起点。我的应用程序找不到某些模板。

我的项目以标准方式设置:

/src
 /main
  /java
   /com.example
    MyWebApplication.java
    HomeController.java
  /resources
   /static
    /css
   /templates
    /fragments
     layout.html
    home.html
  application.properties

波姆。xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>                   
</dependencies>

MyWebApplication.java

@SpringBootApplication
public class MyWebApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyWebApplication.class, args);
    }
}

家庭控制器。JAVA

@Controller
public class HomeController {

    @RequestMapping("/")
    public String home() {
        return "home";
    }
}

home.html

<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org" 
      xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout"
     th:replace="~{fragments/layout :: layout (~{::body},'home')}">
<body>
    <h2 th:text="#{welcome}">Welcome</h2>
        <div class="row">
        </div>
</body>

</html>

layout.html

<!DOCTYPE html>

<!-- 
    The main layout fragment that defines the overall layout
    Sets up the navigation bar
    The page contents are replaced in the main div
 -->
<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout"
        th:fragment="layout (template, menu)">

<head>

  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- <link rel="shortcut icon" type="image/x-icon" th:href="@{/images/favicon.png}"> -->  

  <title>Web Interface</title>

  <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}" />

</head>

<body>

  <!-- Navigation Bar -->
  <nav class="navbar navbar-default" role="navigation">
    <div class="container">
      <div class="navbar-header">
        <a class="navbar-brand glyphicon glyphicon-home" th:href="@{/}"></a>

      <!-- Collapse the menu bar on small windows -->
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#main-navbar">
                  <span class="sr-only">Toggle navigation</span>
                  <span class="icon-bar"></span>
                  <span class="icon-bar"></span>
                  <span class="icon-bar"></span>         
        </button>      
      </div>

      <!-- The Navbar items -->
      <div class="navbar-collapse collapse" id="main-navbar">
        <ul class="nav navbar-nav navbar-right">

          <!-- Define a fragment for each menu item -->
          <li th:fragment="menuItem (path, active, title, glyph, text)" th:class="${active==menu ? 'active' : ''}">
            <a th:href="@{__${path}__}" th:title=${title}>
              <span th:class="'glyphicon glyphicon-'+${glyph}" class="glyphicon glyphicon-home" aria-hidden="true"></span>
              <span th:text="${text}">Template</span>
            </a>
          </li>

          <!-- Replace with the fragment -->
          <li th:replace="::menuItem ('/', 'home', 'home page', 'home', 'Home')">
            <span class="glyphicon glyphicon-home" aria-hidden="true"></span>
            <span> Home</span>
          </li>                   
        </ul>
      </div>
    </div>
  </nav>

  <!-- Main body -->
  <div class="container-fluid">
    <div class="container xd-container">
      <div th:replace="${template}"></div>
    </div>

  </div>

  <script th:src="@{/js/bootstrap.min.js}" type="text/javascript"></script>

</body>

</html>

application.properties是空的。

当我导航到http://localhost:8080时,我得到错误:

解析模板“{fragments/layout”时出错,模板可能不存在,或者任何已配置的模板解析程序都无法访问该模板(主页:5)

如果我从主目录中删除“th:replace”属性。html,它将显示欢迎消息。

浏览类似的问题(例如这个问题),我看到其他人创建了ThymeleafViewResolver bean。

问题:

  1. 为什么找不到我的模板?
  2. 是否需要创建模板解析器?如果是这样,为什么宠物诊所的样本没有?它的模板解析器从哪里来?

据我所知,它的设置方式与宠物诊所样本相同,那么有什么区别呢?

共有1个答案

施旭东
2023-03-14

Spring Boot自动为您完成一些内部工作,但是要完成自动工作,您需要正确配置它。这是问题2的答案,如果配置正确,Spring会自动提供视图解析器。好的,现在让我们进入问题1。

如果你比较你的pom。xml与petclinic的xml有一些关键区别。

首先,根据spring官方文件http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-build-systems.html#using-bootmaven,maven用户可以继承springbootstarter父项目以获得合理的默认值。父项目提供了一些功能。要将项目配置为从SpringBootStarter父级继承,只需在pom中设置父级。xml。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.1.RELEASE</version>
</parent>

使用该设置,您还可以通过覆盖自己项目中的属性来覆盖各个依赖项。例如,在petclinic pom中。xml,他们指定了您的中缺少的thymeleaf版本。

<thymehtml" target="_blank">leaf.version>3.0.2.RELEASE</thymeleaf.version>

现在,为了正确地设置Thymeleaf和Thymeleaf布局方言版本,并使它们之间具有兼容性,您需要从spring启动程序Thymeleaf中排除Thymeleaf布局方言,将Thymeleaf版本设置为3.0的bcz。2.发布版本。

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
      <exclusions>
        <exclusion>
            <groupId>nz.net.ultraq.thymeleaf</groupId>
            <artifactId>thymeleaf-layout-dialect</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
 类似资料:
  • 已经讨论过这些话题,但没有对我起作用: Topic1我的项目中没有注释 这是我用于Thymeleaf的依赖项: 我的新控制器名为 名为uploadView的HTML。html 项目结构: 我的评论:我仍然得到一个白标签错误页面,位于本地主机:8082/uploadendpoint 编辑1: 我的SpringBootApplication类 <代码>应用程序。属性文件: 服务器的日志 白标错误页面:

  • 导出为HTML会生成以下内容: 报告在页面上居中,但应左对齐。 使用JRHtmlExporter的HTML\u HEADER参数看起来很有希望,但这些类已被弃用。这就是解决方案: 现在我必须使用网络。旧金山。jasperreports。出口HtmlExporter和net。旧金山。jasperreports。出口SimpleHtmlReportConfiguration类,如下所示: 您将如何修复

  • 过滤完成后,如果列表值与输入值均不匹配,我将尝试显示“未找到”文本。我使用了不同的方法,但不起作用,下面是我的代码。提前谢谢你。 我使用了一种方法,在html上添加了div text,并将其分类为“text”,样式为“display:none”,然后在js:document中的“else”下面添加了这段代码。getElementById(“文本”)。风格显示=“无”。之后,当您在输入中键入内容时,

  • 我试图在HTML的pre标签中包装文本,但它不起作用。我使用下面的CSS作为我的标签。 我从如何在pre标记中换行文本? 我已添加

  • 这困扰我太久了,我很感激你的帮助。我一直在为kable投入时间,但它并没有像我希望的那样对我产生效果。我希望创建多组行,如图所示 执行此操作的代码如下: 这显然不够好。如果我织成pdf,我可以得到正确的输出(第一张图片),但仅此而已。如果我尝试执行save_kable(),结果显示为HTML格式,如第二幅图像所示。每次都编织成pdf格式是如此不切实际,如果我不能解决这个问题,我就不能再使用kabl

  • 在我的。java文件,我有 在我的第1页。html文件: 现在,my page1显示: 是否可以将我的结果变量呈现为html,以便我的page1显示一个大的绿色通行证?除了th: text${var}之外,还有其他格式选项吗?我正在使用Spring boot和thymeleaf。我尽量不使用javascript。 类似这样的东西,但对于java