错误显示在一个简单的Thymeleaf Spring Boot应用程序中的HTML。

huangapple go评论69阅读模式
英文:

Error displaying HTML with a simple thymeleaf Spring boot application

问题

我正在尝试使用Spring和Thymeleaf显示一个HTML文件,但当我从浏览器访问控制器时,出现以下错误:

org.thymeleaf.exceptions.TemplateInputException: 无法解析模板[error],模板可能不存在,或无法被任何配置的模板解析器访问
	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]

以下是我的 Controller 代码:

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>
    
</project>

这是 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 '"></p>
</body>

</html>

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

英文:

I am trying to display a HTML file with Spring and Thymeleaf but when I hit the controller from the browser, I get error shown as below:

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]

below is my Controller code..

/*
* 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=&quot;/show&quot;)
public String stamp(){
return &quot;helloworld&quot;;
}
}

this is my application.properties file:

server.error.whitelabel.enabled=false

this is my pom.xml file:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;2.3.4.RELEASE&lt;/version&gt;
&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;
&lt;groupId&gt;com.dafe&lt;/groupId&gt;
&lt;artifactId&gt;thymeLeafAppDemo&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;name&gt;thymeLeafAppDemo&lt;/name&gt;
&lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
&lt;properties&gt;
&lt;java.version&gt;11&lt;/java.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
&lt;scope&gt;runtime&lt;/scope&gt;
&lt;optional&gt;true&lt;/optional&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;exclusions&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
&lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;/exclusions&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;

</project>

below is the Main Spring boot class:

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);
}
}

the file I am trying to display is a helloworld.html file and this is the path src/main/resources/templates/helloworld.html. The url I used on the browser is http://localhost:8080/show.

This is the content of the file:

&lt;!DOCTYPE HTML&gt;
&lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
&lt;head&gt;
&lt;title&gt;Thymeleaf Demo&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p th:text=&quot;&#39;Time on the server is &#39; &quot;/&gt;
&lt;/body&gt;
&lt;/html&gt;

Please help me fix this issue... it is my first time with thymeleaf. Thanks.

答案1

得分: 2

我终于弄清楚了答案,所以我想分享出来,以便将来可以让其他人受益。我遇到问题的原因是因为 Spring Boot 找不到我的控制器类。默认情况下,Spring Boot 会对主 Spring 应用程序的当前包及所有子包进行组件扫描。

我的主 Spring 应用程序的包是:com.myThymeLeafDemo

然而,我的控制器不在正确的子包中。控制器当前在包:com.myThymeLeafDemoController。然而,它应该在 com.myThymeLeafDemo 的子包中。

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

旧名:com.myThymeLeafDemoController
新名:com.myThymeLeafDemo.Controller

请注意"."... 这使它成为主包的子包。现在 Spring Boot 能够找到我的控制器了。一旦我运行我的应用程序,它就能按预期工作。

英文:

I finally figured the answer out so I thought to share for the benefit of others in the future. The reason I was having the issue is because Spring Boot could not find my controller class. By default, Spring Boot will component scan the current package of main spring app and all sub-packages.

The package of my main spring app is: com.myThymeLeafDemo

However, my controller is not in a proper sub-package. The controller is current in package: com.myThymeLeafDemoController. However, it should be in a sub-package of com.myThymeLeafDemo

I resolved this by refactoring the package name:

old name: com.myThymeLeafDemoController

new name: com.myThymeLeafDemo.Controller

make note of the "." ... this makes it a subpackage of the main package. Now Spring Boot is be able to find my controller. Once I run my app then it works as desired.

huangapple
  • 本文由 发表于 2020年9月25日 00:12:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64050347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定