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         
2
3 org.springframework.boot
4 spring-boot-starter-thymeleaf
5

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.文件结构

Error resolving template [index], template might not exist or migh_html

解决方案:

确定自己的pom.xml文件中是否添加resources插件,若是没有进行以下步骤检查

解决办法:
1.检查controller中的注解:@RestController是@ResponseBody和@Controller的组合这个注解,返回html页面时应该使用@Controller注解。
2.检查return中的字符串是否有错误(注意没有 ‘/’ 字符)
3.检查html页面是否存在
4.html文件是否存在templates目录下,若是存在自定义的目录下则需要配置

若是有resources插件,配置如下

解决办法:
检查自己的resource标签中是否加入了以html为后缀的资源文件

1             
2 src/main/resources
3
4 **/*.xml
5 META-INF/app.properties
6 application.properties // 加入配置文件
7 **/*.html //加入 html 文件
8

9 true
10

最后重新 update 一下项目即可。