【问题标题】:Error displaying HTML with a simple thymeleaf Spring boot application使用简单的 thymeleaf Spring 启动应用程序显示 HTML 时出错
【发布时间】:2023-04-10 09:35:01
【问题描述】:

我正在尝试使用 Spring 和 Thymeleaf 显示 HTML 文件,但是当我从浏览器中点击控制器时,出现如下所示的错误:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template [error], template might not exist or might not be accessible by any of the configured Template Resolvers
    at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:189) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1373) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    

下面是我的控制器代码..

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thymeLeafAppDemoController;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;


@Controller
public class DemoController {
        
    @GetMapping(value="/show")
    public String stamp(){
        
        
        return "helloworld";
    }
    
}


这是我的 application.properties 文件:

server.error.whitelabel.enabled=false

这是我的 pom.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dafe</groupId>
<artifactId>thymeLeafAppDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>thymeLeafAppDemo</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>11</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-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

下面是 Main Spring boot 类:

package com.thymeLeafAppDemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ThymeLeafAppDemoApplication {

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

}


我要显示的文件是 helloworld.html 文件,这是路径 src/main/resources/templates/helloworld.html。我在浏览器上使用的url是http://localhost:8080/show。

这是文件的内容:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <title>Thymeleaf Demo</title>
</head>

<body>

<p th:text="'Time on the server is ' "/>

</body>

</html>

请帮我解决这个问题...这是我第一次使用百里香。谢谢。

【问题讨论】:

  • 您的设置看起来正确。您使用的是 Maven 还是 Gradle?
  • 我正在使用 Maven...@chrylis
  • 请显示您的target/classes/templates 目录的内容。
  • 我没有 target/classes/templates 目录,我有的是 src/main/resources/templates/helloworld.html ... @chrylis
  • 我没有 target/classes/templates 目录,我拥有的是 src/main/resources/templates 并且在该目录中,我有文件 helloworld.html @chrisly

标签: java spring-boot thymeleaf


【解决方案1】:

我终于想出了答案,所以我想在未来分享给他人。我遇到问题的原因是因为 Spring Boot 找不到我的控制器类。默认情况下,Spring Boot会组件扫描主spring app的当前包和所有子包。

我的主要spring应用的包是:com.myThymeLeafDemo

但是,我的控制器不在适当的子包中。控制器在包中是最新的:com.myThymeLeafDemoController。但是,它应该在 com.myThymeLeafDemo 的子包中

我通过重构包名解决了这个问题:

旧名称:com.myThymeLeafDemoController

新名称:com.myThymeLeafDemo.Controller

记下“。” ...这使它成为主包的子包。现在 Spring Boot 能够找到我的控制器。一旦我运行我的应用程序,它就会按需要运行。

【讨论】:

    猜你喜欢
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 2018-11-19
    相关资源
    最近更新 更多