IDEA| Error resolving template [index], template might not exist or might not be accessible by any

本文解决了一个常见的SpringBoot与Thymeleaf整合时出现的问题,即后台Controller返回页面地址时提示找不到index.html。文章详细介绍了错误的原因及解决方案,包括添加依赖、配置环境及前端页面修改。

报错: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">即可解决问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值