参考
https://www.jianshu.com/p/acee38389266
https://blog.csdn.net/h1021456873/article/details/79085505
SpringBoot中使用Thymeleaf模板,需要在项目的application.properties文件中进行相关配置,如下:
spring.thymeleaf.prefix=classpath:webapp/views/ #配置静态页面所在的位置,这里静态文件的位置为resources/webapp/views/
注意啦,前端使用thymeleaf对应的th标签引用其他页面或模块时,需要结合application.properties中配置的路径来对应上指定的文件路径。
下面是application.properties的配置参考:
实际应用中,遇到过这个问题,主要原因有可能是指定文件不存在,或者文件路径配置错误(路径要参考配置的路径进行拼接):
参考:https://www.oschina.net/question/1251858_2235767
TemplateInputException: Error resolving template "xx", template might not exist
or might not be accessible by any of the configured Template Resolvers (template:"views/index" ...)
下面是相关前端页面的代码:
1. structure.html (公共模块文件,用于定义一些公用模块)
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<!--定义引用模块-->
<div class="layui-header" th:fragment="navBar">
<!--模块内容-->
</div>
2.index.html (引用定义好的模块Demo,相当于jsp中的include功能)
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body>
<!-- 引用 webapp/views/templates/structure.html 中的navBar模块 -->
<!-- replace表示替换当前div,使用include则表示引用 -->
<div th:replace="templates/structure :: navBar">
</body>
</html>