报错:Error resolving template [index], template might not exist or might not be accessible by any of the configured.
使用环境:使用shpringboot搭建项目时候,后台COntroller返回页面地址报错提示找不到index.html。
原因:在使用springboot的过程中,如果使用thymeleaf作为模板文件,则要求HTML格式必须为严格的html5格式,必须有结束标签,否则会报错。
解决方案:
1、添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、配置环境
#thymelea模板配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
spring.mvc.static-path-pattern=/static/**
3、(重要!!!)前端页面修改
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>kevin-视频播放</title>
<link rel="shortcut icon" href="/static/bootstrap/favicon.ico" type="image/x-icon">
<link type="text/css" rel="stylesheet" href="/static/bootstrap/css/bootstrap.css"/>
<link type="text/css" rel="stylesheet" href="/static/bootstrap/dfcss/video.css"/>
<script type="text/javascript" src="/static/bootstrap/libs/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="/static/bootstrap/libs/bootstrap.js"></script>
<script type="text/javascript" src="/static/bootstrap/libs/bootstrap-paginator.min.js"></script>
<script type="text/javascript" src="/static/bootstrap/js/video.js"></script>
</head>
<body>
Note: html默认是<html lang="en">格式,就是这种格式导致无法解析为thymeleaf。将页头改为<html lang="en" xmlns:th="http://www.thymeleaf.org">即可解决问题。
本文解决了一个常见的SpringBoot与Thymeleaf整合时出现的问题,即后台Controller返回页面地址时提示找不到index.html。文章详细介绍了错误的原因及解决方案,包括添加依赖、配置环境及前端页面修改。
1万+

被折叠的 条评论
为什么被折叠?



