英文:
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="/show")
public String stamp(){
return "helloworld";
}
}
this is my application.properties file:
server.error.whitelabel.enabled=false
this is my pom.xml file:
<?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>
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:
<!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>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论