Error resolving template [index], template might not exist or might not be accessible by any of the configured Template Resolvers

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 一下项目即可。

posted @ 2020-09-29 14:15  明天,你好啊  阅读(3365)  评论(0编辑  收藏  举报