我有以下控制器:
@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)
uj5u.com熱心網友回復:
問題出在您的 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 中洗掉它并洗掉您的目標目錄。
uj5u.com熱心網友回復:
<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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/481916.html
