我有以下控制器:
@Controller
public class ConfigController {
@GetMapping("/")
public String index() {
return "config";
}
}
但是,我在前往路线时收到以下错误
/
:
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template [config], template might not exist or might not be accessible by any of the configured Template Resolvers
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [config], template might not exist or might not be accessible by any of the configured Template Resolvers
我在
config.html
中有一个 resources/templates
文件:
我的pom.xml文件:https://pastebin.com/yqYYuXWh
我做错了什么?
我已经尝试在控制器函数的返回中添加
.html
,但仍然不起作用。
删除 spring-web 依赖项后(我已经有了
spring-boot-starter-web
),现在启动 spring 时出现以下错误:
Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
问题出在您的 pom.xml 文件中。
将此块添加到您的
build
;
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.properties</include>
</includes>
</resource>
</resources>
您已经覆盖了
spring-boot-starter-parent
自己的 resources
块,并使其包含 application.properties
而没有其他内容。从您的 pom.xml 中删除它并删除您的目标目录。
如果有人满足了所有要求,这是一个修复。检查方法的返回值。如果你喜欢
return '/admin/index'
将其更改为 return 'admin/index
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.20</version>
</dependency>
第二个依赖项不是必需的。
<build>
<!--not required. Spring boot scans /resources directory by default
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.properties</include>
</includes>
</resource>
</resources>
-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
请重新构建并重新启动应用程序。
并且您不需要在控制器中添加
.html
扩展。 Spring Boot 默认在 .html
、.jsp
、src/main/resource/
、src/main/resource/templates/
这些目录中查找 src/main/resource/static/
、src/main/resource/public
扩展
使用 @ResponseBody 注释来注释您的处理程序方法,如下所示 public @ResponseBody 字符串索引() @ResponseBody 注释告诉控制器返回的对象会自动序列化为 JSON 并传回 HttpResponse 对象。当您在方法上使用 @ResponseBody 注解时,Spring 会自动转换返回值并将其写入 HTTP 响应。