jsp不支持jar包 jsp
Thymeleaf最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个Web应用。它的功能特性如下:
- Spring MVC中@Controller中的方法可以直接返回模板名称,接下来Thymeleaf模板引擎会自动进行渲染
- 模板中的表达式支持Spring表达式语言(Spring EL)
- 表单支持,并兼容Spring MVC的数据绑定与验证机制
- 国际化支持
- Thymeleaf中若使用了
<iframe>
布局就完全不能使用Vue.js了 - Thymeleaf渲染的页面放在
resources/templates
目录下,这样Thymeleaf会默认识别。Thymeleaf渲染的页面放在resources/templates
目录下,这样Thymeleaf会默认识别。 - HTML页面中使用Thymeleaf,需要修改
<html lang="en">
为<html lang="en" xmlns:th="http://www.thymeleaf.org">
- spring boot项目中
resources/static
目录下的文件可通过浏览器直接访问,而resources/templates
下的HTML不能通过浏览器直接访问,而需要Spring Mvc
这样的框架映射到那个页面地址。
第一步:pom.xml
<!--避坑包-->
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
<!--解析html包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
第二步:配置application.properties
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties类里面有thymeleaf的默认配置。
注意 1.结尾一定要有------ #thymeleaf end --------- 否则掉坑
2.#模板编码 spring.thymeleaf.mode=LEGACYHTML5
要想使用LEGACYHTML5这个编码格式必须引入 上面pom中‘避坑包’ 否则用不了
为什么不用HTML5?
因为在默认配置下,thymeleaf对.html的内容要求很严格,比如<meta charset=”UTF-8″ />,
如果少最后的标签封闭符号/,就会报错而转到错误页。因此,建议增加下面这段:
spring.thymeleaf.mode = LEGACYHTML5,默认值是HTML5,其实很严格,改为LEGACYHTML5可以得到一个可能更友好亲切的格式要求。
#thymeleaf start
#<!-- 关闭thymeleaf缓存 开发时使用 否则没有实时画面,默认为true-->
spring.thymeleaf.cache=false
## 检查模板是否存在,然后再呈现
spring.thymeleaf.check-template-location=true
spring.thymeleaf.check-template=true
spring.thymeleaf.encoding=utf-8
#LEGACYHTML5是相比默认的html5更友好的模板编码
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.servlet.content-type=text/html
#MVC thymeleaf 视图解析
spring.thymeleaf.enabled=true
## 要被排除在解析之外的视图名称列表,用逗号分隔
##spring.thymeleaf.excluded-view-names=
#前缀,默认值就好了
spring.thymeleaf.prefix=classpath:/templates/
#后缀,默认为.html
spring.thymeleaf.suffix=.html
# Thymeleaf模板解析器在解析器链中的顺序。默认情况下,它排第一位。顺序从1开始,只有在定义了额外的TemplateResolver Bean时才需要设置这个属性。
#spring.thymeleaf.template-resolver-order=
# 可以解析的视图列表名称,逗号分隔
#spring.thymeleaf.view-names=
#thymeleaf.end
第三步:controller层
注入的时候一定要是Controller 不要是RestController 因为它是rest接口(json格式) 是解析不到html
@Controller和@RestController的区别?
第四部:templates/hello.html
红波浪线别管雅婷的。Spring boot 在使用 Thymeleaf 时 Intellij idea model 属性没法自动解析?
常用的th:
常用th标签都有那些?
关键字 功能介绍 案例
th:id 替换id <input th:id="'xxx' + ${collect.id}"/>
th:text 文本替换 <p th:text="${collect.description}">description</p>
th:utext 支持html的文本替换 <p th:utext="${htmlcontent}">conten</p>
th:object 替换对象 <div th:object="${session.user}">
th:value 属性赋值 <input th:value="${user.name}" />
th:with 变量赋值运算 <div th:with="isEven=${prodStat.count}%2==0"></div>
th:style 设置样式 th:style="'display:' + @{(${sitrue} ? 'none' : 'inline-block')} + ''"
th:onclick 点击事件 th:οnclick="'getCollect()'"
th:each 属性赋值 tr th:each="user,userStat:${users}">
th:if 判断条件 <a th:if="${userId == collect.userId}" >
th:unless 和th:if判断相反 <a th:href="@{/login}" th:unless=${session.user != null}>Login</a>
th:href 链接地址 <a th:href="@{/login}" th:unless=${session.user != null}>Login</a> />
th:switch 多路选择 配合th:case 使用 <div th:switch="${user.role}">
th:case th:switch的一个分支 <p th:case="'admin'">User is an administrator</p>
th:fragment 布局标签,定义一个代码片段,方便其它地方引用 <div th:fragment="alert">
th:include 布局标签,替换内容到引入的文件 <head th:include="layout :: htmlhead" th:with="title='xx'"></head> />
th:replace 布局标签,替换整个标签到引入的文件 <div th:replace="fragments/header :: title"></div>
th:selected selected选择框 选中 th:selected="(${xxx.id} == ${configObj.dd})"
th:src 图片类地址引入 <img class="img-responsive" alt="App Logo" th:src="@{/img/logo.png}" />
th:inline 定义js脚本可以使用变量 <script type="text/javascript" th:inline="javascript">
th:action 表单提交的地址 <form action="subscribe.html" th:action="@{/subscribe}">
th:remove 删除某个属性 <tr th:remove="all">
1.all:删除包含标签和所有的孩子。
2.body:不包含标记删除,但删除其所有的孩子。
3.tag:包含标记的删除,但不删除它的孩子。
4.all-but-first:删除所有包含标签的孩子,除了第一个。
5.none:什么也不做。这个值是有用的动态评估。
th:attr 设置标签属性,多个属性可以用逗号分隔 比如 th:attr="src=@{/image/aa.jpg},title=#{logo}",此标签不太优雅,一般用的比较少。
thymeleaf整合shiro的依赖:
thymeleaf-extras-shiro最新版本是2.0.0,配置使用报错,所以使用1.2.1版本;
该jar包的github地址:https://github.com/theborakompanioni/thymeleaf-extras-shiro
/**
* @描述:ShiroDialect,为了在thymeleaf里使用shiro的标签的bean
*
*/
@Bean
public ShiroDialect shiroDialect(){
return new ShiroDialect();
}
- 页面中使用
<html xmlns:th="http://www.thymeleaf.org"
xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
... ...
<!-- 获取shiro中登录的用户名 -->
<shiro:principal property="username"></shiro:principal>
更多
https://github.com/theborakompanioni/thymeleaf-extras-shiro