ssm中使用jsp作为前端页面
springboot不支持jsp但是支持模板引擎+html
缓存
在application.properties配置文件中关闭模板的缓存 避免开发的过程中有了变更但是刷新不出来 等真正上线的时候再打开
# ThymeleafProperties
spring.thymeleaf.cache=false
配置
将模板和数据结合一起 替换变量显示出来页面
springboot2.x只需要导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
使用
- 在控制层直接拼接 return转到classpath:/templates/路径下的 html前端页面
- 在html页面当中的使用:
- 需要导入语法提示
<html lang="en" xmlns:th="http://www.thymeleaf.org">
2. 用法
属性
5种表达式
$()
比如获取model and view中的参数
#()
<b th:text="${#dates.format(map.post.createTime,'yyyy-MM-dd HH:mm:ss')}">2019-04-15 15:32:18</b>
模板内置对象 通过#来调用 接着用${#内置对象.属性}来使用属性
*{}
和${}类似 但是有优化的一点
优秀于
@{}
定义url链接
~{}
引用
一个分页的模板实例
里面包含了链接 获取参数 使用封装工具还有判断等
<!-- 分页 -->
<nav class="mt-5" th:if="${page.rows>0}">
<ul class="pagination justify-content-center">
<li class="page-item">
<a class="page-link" th:href="@{${page.path}(current=1)}">首页</a>
</li>
<li th:class="|page-item ${page.current==1?'disabled':''}|">
<a class="page-link" th:href="@{${page.path}(current=${page.current-1})}">上一页</a></li>
<li th:class="|page-item ${i==page.current?'active':''}|" th:each="i:${#numbers.sequence(page.from,page.to)}">
<a class="page-link" href="#" th:text="${i}">1</a>
</li>
<li th:class="|page-item ${page.current==page.total?'disabled':''}|">
<a class="page-link" th:href="@{${page.path}(current=${page.current+1})}">下一页</a>
</li>
<li class="page-item">
<a class="page-link" th:href="@{${page.path}(current=${page.total})}">末页</a>
</li>
</ul>
</nav>
- 可以使用${参数}(参数后面要拼接的)来表示正常的拼接 这是 ${}的一个功能
使用工具类
th 和class 放在一起