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

Springboot+Thymeleaf:解析模板失败

东方震博
2023-03-14

我想在HTML中显示一个请求的对象,我得到了一个错误,我不知道是什么原因导致了我的错误。谷歌帮不上忙,现在我试着问你。我认为错误不是来自我的表,因为我把它注释掉了,错误仍然是Occour。该错误也不是来自“http://localhost:8081/simulation”,因为我使用有效值重新接收了一个有效的JSON。谢谢你的帮助:)。

下面是我的代码:

package de.WI18C.Gruppe5.API.controller;
import de.WI18C.Gruppe5.API.model.Unternehmen;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@Controller
public class APIContoller {

@RequestMapping(value = "/unternehmen", method = RequestMethod.GET)
public String viewUnternehmen(Model model) {
    RestTemplate restTemplate = new RestTemplate();
    Unternehmen unternehmen = restTemplate.getForObject("http://localhost:8081/simulation", Unternehmen.class);
    model.addAttribute("unternehmen", unternehmen);

    return "unternehmen";
   }
}
<!doctype html>
<html xmlns:th="http://www.thymeleaf.org" lang="de">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/>

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
          integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

    <title>Unternehmen</title>
</head>
<body>

<div class="col-12 align-content-center">
    <div class="row">
        <div class="col-8 mx-auto">
            <table class="table">
                <thead>
                <tr>
                    <th scope="col">#</th>
                    <th scope="col">Attribut</th>
                    <th scope="col">Wert</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <th scope="row">1</th>
                    <td>Name</td>
                    <td th:text="${unternehmen.getName()}"></td>
                </tr>
                <tr>
                    <th scope="row">2</th>
                    <td>Bestand</td>
                    <td th:text="${unternehmen.getBestand()}"></td>
                </tr>
                <tr>
                    <th scope="row">3</th>
                    <td>Bedarf</td>
                    <td th:text="${unternehmen.getBedarf()}"></td>
                </tr>
                <tr>
                    <th scope="row">4</th>
                    <td>Verkaufspreis</td>
                    <td th:text="${unternehmen.getVerkaufspreis()}"></td>
                </tr>
                <tr>
                    <th scope="row">5</th>
                    <td>Gewinn</td>
                    <td th:text="${unternehmen.getGewinn()}"></td>
                </tr>
                <tr>
                    <th scope="row">6</th>
                    <td>Bestand - Rad</td>
                    <td th:text="${unternehmen.getBestand_rad()}"></td>
                </tr>
                <tr>
                    <th scope="row">7</th>
                    <td>Bestand - Rahmen</td>
                    <td th:text="${unternehmen.getBestand_rahmen()}"></td>
                </tr>
                <tr>
                    <th scope="row">8</th>
                    <td>Bestand - Lenker</td>
                    <td th:text="${unternehmen.getBestand_lenker()}"></td>
                </tr>
                <tr>
                    <th scope="row">9</th>
                    <td>Bestand - Licht</td>
                    <td th:text="${unternehmen.getBestand_licht()}"></td>
                </tr>
                <tr>
                    <th scope="row">10</th>
                    <td>Bestand - Schaltung</td>
                    <td th:text="${unternehmen.getBestand_schaltung()}"></td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
        integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
        crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"
        integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
        crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"
        integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
        crossorigin="anonymous"></script>
</body>
</html>

我通过调用“http://localhost:8080/unternehmen”得到的错误(很抱歉格式错误):

2020-12-16 15:07:55.635错误3696---[nio-8080-exec-1]org.THYMELEAF.templateEngine:[THYMELEAF][http-nio-8080-exec-1]异常处理模板“unternehmen”:模板分析期间出错(模板:“class path resource[templates/unternehmen.html]”)

2020-12-16 15:07:55.638错误3696---[nio-8080-exec-1]O.A.C.C.C.[.[.[/].[dispatcherServlet]:路径为[]的上下文中servlet[dispatcherServlet]的servlet.service()引发异常[请求处理失败;嵌套异常为org.thymeleaf.exceptions.TemplateInputException:模板解析期间出错(template:“class path resource[templates/unternehmen.html]”),其根本原因是

共有1个答案

荆树
2023-03-14

你的模板有问题。我无法在本地复制它,但该错误意味着某些模板标记在解析时不正确。可悲的是,Tymeleaf的错误并没有得到很好的传达。

您可以尝试以下两种方法:

A)调试

    null

祝你好运!

 类似资料:
  • 本文向大家介绍SpringBoot使用thymeleaf模板过程解析,包括了SpringBoot使用thymeleaf模板过程解析的使用技巧和注意事项,需要的朋友参考一下 这篇文章主要介绍了SpringBoot使用thymeleaf模板过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.导入依赖 2.application.yml文件中新

  • 下面是index.html 它们在不同的文件夹中,但pathing应该工作,除非我只是错过了一些真正愚蠢的东西。

  • 本文向大家介绍springboot中thymeleaf模板使用详解,包括了springboot中thymeleaf模板使用详解的使用技巧和注意事项,需要的朋友参考一下 这篇文章将更加全面详细的介绍thymeleaf的使用。thymeleaf 是新一代的模板引擎,在spring4.0中推荐使用thymeleaf来做前端模版引擎。 thymeleaf介绍 简单说, Thymeleaf 是一个跟 Vel

  • 本文向大家介绍SpringBoot中的Thymeleaf模板,包括了SpringBoot中的Thymeleaf模板的使用技巧和注意事项,需要的朋友参考一下 一、前言     Thymeleaf 的出现是为了取代 JSP,虽然 JSP 存在了很长时间,并在 Java Web 开发中无处不在,但是它也存在一些缺陷: 1、JSP 最明显的问题在于它看起来像HTML或XML,但它其实上并不是。大多数的JS

  • 我需要使用Thymeleaf模板为不同地区发送电子邮件。 我的HTML中有下一部分 我有下一个密码 我有下一个豆子 我正在使用下一个依赖项 我在“资源”文件夹中有我需要的每种语言的资源包消息。如果你用ctrl左键从我的html中检查#{TEST},你可以看到它看到属性文件,所以IDEA可以解析它。 当我运行我的应用程序并尝试发送电子邮件时,我得到了下一个结果 ??TEST_de?? 对于其他语言,

  • 我的问题是我未能显示胸腺叶模板。我怀疑配置错误,但我似乎找不到它。提前感谢:) 波姆。xml: 模板在: Spring配置: 控制器: 我正在尝试访问:并获得404。 另一件事,有人能解释(或给文档一个链接)哪个servlet被用来“返回”模板html吗?是Spring调度员servlet吗? 好的,所以我忘记了,谢谢@Benjamin c.然而,添加它会产生: