一:Thymeleaf使用步骤
1.创建SpringBoot项目,添加Thymeleaf依赖
2.在resource中找到templates文件
3.创建html文件,引入thymeleaf命名空间,
<html lang="en" xmlns:th="http://www.thymeleaf.org">, 方便使用thymeleaf属性
二:Thymeleaf进行变量的输出
1.th:text,将model中的值作为内容放入标签中
2.th:value,将model中的值放入input标签中的value属性中
三:
1.Thymeleaf提供了一些内置对象可以操作数据,内置对象可直接在模板中使用,这些对象是以#引用的,操作字符串的内置对象为strings。
${#strings.isEmpty(key)}:判断字符串是否为空,如果为空,则返回true,否则返回false
${#strings.length}:返回字符串的长度
${#strings.strings.indexof(msg,'h')}:查找子串的位置,没找到则返回-1
。。。还有很多
2.thfmeleaf获取request、session、application域中的数据
request:<span th:text="${#request.getAttribute('req')}"> 或者
<span th:text="${#HttpServletRequest.getAttribute('req')}">
Session:<span th:text="${session.ses}"> 或者
<span th:text="${#HttpSession.getAttribute.('ses')}">
application:<span th:text="${application.app}" > 或者
<span th:text="${#servletContext.getAttribute('app')}">
3.在thymeleaf中路径的写法为<a th:href="@{http://www.baidu.com}">百度</a>
路径也可以是跳转到别的页面:
1.<a th:href="@{show2?id=1&name=aaa}">静态参数</a>
<a th:href="@{show2(id=1,name=aaa)}">静态参数</a>
<a th:href="@{show2(id=${id},name=${name})}">动态参数</a>
注:1.thymeleaf支持el表达式
2.单纯打开thymeleaf页面时,是一个html页面。在启动了项目,跳转到这个页面时,可以动态的展示数据