因为springboot不支持jsp
使用模板引擎
写一个页面模板,和数据交给模板,生成一个页面。
推荐thymeleaf
语法简单功能强大,
1.引入thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
修改版本:
<properties>
<java.version>1.8</java.version>
<thymeleaf-spring5.version>3.0.9.RELEASE</thymeleaf-spring5.version>
<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
</properties>
2.使用语法:
按照自动配置规则来使用
只要我们把html页面放在templates里,就能自动渲染了,
在html文件中使用thymeleaf调用
首先导入thyme leaf提示
<html lang="en" xmlns:th="http://www.thymeleaf.org">
使用thymeleaf的语法
th:text="":将div中文本内容设置为指定内容
语法规则:
th:任意html属性,就可以直接在后台之类的取值
片段包含:
th:insert
th:replace
遍历:
th:each
条件判断:th:if
th:switch
th:unless
th:case
变量声明
th:object
th:with
属性修改
th:attr
th:attrprepend
th:attrappend
修改指定属性:
th:value
th:href
th:src
修改标签体内容
th:text
th:utext 不转义特殊字符
声明片段
th:fragmen
移除
th:remove
2.表达式
${}获取变量值,获取对象的属性,调用方法,使用内置基本对象,内置的工具对象。
*{}选择表达式和${}差不多但是有补充,
#{}国际化内容
@{}定义url链接的
~{}片段引用的表达式
<hr/> <div th:text="${he}"></div> <div th:utext="${he}"></div> <hr/> <h4 th:text="${user}" th:each="user:${users}"}></h4>//users的值赋给user了然后显示 <hr/> <h4> <span th:each="user:${users}">[[${user}]]</span>//[[]]这个直接代表th:text </h4>