springboot 整合 html 頁面,無法加載到 .html 頁面。
錯誤描述
[2020-09-29 14:01:37.541] [http-nio-8888-exec-1] [ERROR][org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet]] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template [index], template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [index], template might not exist or might not be accessible by any of the configured Template Resolvers
1.maven 依賴
1 <!-- Thymeleaf --> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-thymeleaf</artifactId> 5 </dependency>
2.application.properties 配置
1 server.port=8888 2 spring.mvc.view.prefix=/templates/ 3 spring.mvc.view.suffix=.html
3.代碼實現
1 @GetMapping("/index") 2 public String index() { 3 return "index"; 4 }
4.文件結構
解決方案:
確定自己的pom.xml文件中是否添加resources插件,若是沒有進行以下步驟檢查
解決辦法:
1.檢查controller中的注解:@RestController是@ResponseBody和@Controller的組合這個注解,返回html頁面時應該使用@Controller注解。
2.檢查return中的字符串是否有錯誤(注意沒有 ‘/’ 字符)
3.檢查html頁面是否存在
4.html文件是否存在templates目錄下,若是存在自定義的目錄下則需要配置
若是有resources插件,配置如下
解決辦法:
檢查自己的resource標簽中是否加入了以html為后綴的資源文件
1 <resource> 2 <directory>src/main/resources</directory> 3 <includes> 4 <include>**/*.xml</include> 5 <include>META-INF/app.properties</include> 6 <include>application.properties</include> // 加入配置文件 7 <include>**/*.html</include> //加入 html 文件 8 </includes> 9 <filtering>true</filtering> 10 </resource>
最后重新 update 一下項目即可。