jsp
到thymeleaf
,再到vue
,这是一个前后端逐渐分离的过程。jsp
的特点是页面中可以嵌套java代码,这显然很强大,但导致的结果是页面逻辑
(一些前端界面元素的显示隐藏,以及动态变化)和业务逻辑
(一些需要访问后台的操作,如数据库查询)常常纠缠在一起,代码混乱不堪,可维护性差,前后台人员职责分工不明确,责任难以追踪。到了thymeleaf
,情况好了很多,因为thymeleaf
支持html
界面,无论是不是在服务器环境下,界面都能展示,在界面上直接与后台交互的情况也少了很多。再到现在很火的vue
,才算实现了真正意义上的前后端分离,前端人员专注于前台界面,后端人员专注于后台接口。Springboot整合thymeleaf很简单,下面只介绍下几个步骤:
1.导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.简单配置 application.properties
spring.thymeleaf.enabled=true
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.mode=HTML
spring.thymeleaf.suffix=.html
3.在html界面引入名称空间
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
......
</html>
thymeleaf语法都是html标签加th:*
的形式
//输出字符串 thymeleaf
<h2 th:text="'thymeleaf'"></h2>
//输出字符串 thymeleaf
<h2 th:utext="'thymeleaf'"></h2>
th:text和th:utext的区别:
th:utext中的html标签会正常翻译成html标签,而th:text的内容原样输出。如下:
<p th:text="'hello <b>thymeleaf</b>'"></p> //原样输出
<p th:utext="'hello <b>thymeleaf</b>'"></p> //thymeleaf加粗
输出包含空格的文本:
<h2 th:text="'thymeleaf home page'"></h2>
<h2 th:utext="|thymeleaf home page|"></h2>
所有th:标签都可以用data-th-替换:
<h2 data-th-text="|thymeleaf home page|"></h2>
<h2 data-th-utext="|thymeleaf home page|"></h2>
#
号用于国际化
1.在templates
文件夹下建立i18n
目录,新建messages_zh_CN.properties
文件,内容如下:
thymeleaf.welcome=welcome to thymeleaf index page
2.在application.properties
中配置:
spring.messages.basename=i18n/messages
3.在界面中使用:
<p th:text="#{thymeleaf.welcome}"></p>
$
用于后端取值
后端要向model存入user对象
<div th:text="${user.name}"></div>
*
用于绑定一个对象的属性,与$
结合使用
<span th:object="${human}">
姓名:<span th:text="*{name}"></span> <br>
爱好:<span th:text="*{favorite}"></span> <br>
国籍:<span th:text="*{nationality}"></span> <br>
</span>
@
用于链接
<!--引入static/css目录中的thymeleaf.css -->
<link rel="stylesheet" href="../static/css/thymeleaf.css" th:href="@{/css/thymeleaf.css}">
<!--超链接 最终路径为/user/find?username=id-->
<a th:href="@{/user/find(username=${human.name})}">查找用户</a>
<!--最终路径为:/user/find/具体的username-->
<a th:href="@{/user/find/{username}(username=${human.name})}">查找用户</a> <br>
~
用于取到项目根路径
<a th:href="@{~/user/find/jack}">查找用户</a>
<!--修改value属性-->
<input type="submit" value="提交" th:value="submit">
<br>
<!--等价于-->
<input type="submit" value="提交" th:attr="value='submit'">
<hr>
<!--除了th:value还有很多其它html元素的属性都可用。-->
<!--追加样式一个addStyle样式-->
<input type="text" class="originStyle" th:classappend="'addStyle'">
<hr>
<!--设置选择状态-->
<input type="checkbox" th:checked="${isChecked}">
<div th:object="${user}">
<div th:text="*{username} == null ? 'username is not defined' : *{username}"></div>
</div>
三元表达式省略写法,表达式为真,值为*{favorite},否则,值为后面的字符串
<div th:object="${user}">
<div th:text="*{username}?:'username attribute is not defined'"></div>
</div>
三元表达式省略写法,表达式为真,会给标签加上customStyle样式,为假,返回null,什么也不执行
<div th:class="${customStyleOn} ? 'customStyle'"></div>
<div th:switch="${usernames.get(1)}">
username:
<p th:case="'Lorem'">lorem</p>
<p th:case="'ipsum'">ipsum</p>
<p th:case="'dolor'">dolor</p>
<p th:case="*">whatever</p> <!--都不匹配时执行-->
</div>
<!--用#引用内置对象判断字符串是否为空-->
<div th:if="${not #strings.isEmpty('lorem')}">lorem</div>
<div th:unless="${ #strings.isEmpty('lorem')}">lorem</div>
<div th:text="33 + 22"></div>
<div th:text="${number} + 22 > 30"></div>
<ul>
<!--遍历user对象,如果是第偶数个则加上自定义样式,如果是奇数,加上详情连接-->
<li th:each="user : ${users}" th:classappend="${userStat.even}?'m-list-even'">
<span th:text="${user}"></span>
<a th:href="@{/user/find/{username}(username=${user})}" th:if="${userStat.odd}">view detail</a>
</li>
</ul>
fragment类似于jsp的jsp:include,用于重用一些页面。
在templates目录下建立一个footer.html
用于存放通用的版权片段:内容如下:
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<!--copyright是片段的名字-->
<footer th:fragment="copyright">
<div>
©2019-2020 Designed by Oamha, Glad to receive your reply
</div>
</footer>
</html>
下面就可以将其引入到其它界面中了:
<!--footer是片段所在的文件名,copyright是片段名-->
<div th:insert="footer :: copyright"></div>
<!--上面等价于-->
<div th:insert="~{footer :: copyright}"></div>
<!--引入片段也可以用th:replace-->
<div th:replace="footer :: copyright"></div>
<!--还可以用th:include-->
<div th:include="footer :: copyright"></div>
片段还可以传参数:比如有一个navbar片段,所在文件templates/nav.html
:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<div th:fragment="navbar(span, role, username)">
<span th:replace="${span}"></span>
<span th:text="${role}"></span>
<span th:text="${username}"></span>
</div>
</html>
在其他界面引入:
<!--第一个参数传入当前界面的span标签,第二个参数是字符串,第三个参数代表什么也不传-->
<div th:replace="nav :: navbar (~{::span}, 'admin', _)">
<span>这个span将替换片段中的span</span>
</div>
<table>
<!--/*/<th:block th:each="user : ${users}">/*/-->
<tr>
<td th:text="${user}">oamha</td>
</tr>
<!--/*/</th:block>/*/-->
</table>
<!--/*/内容/*/-->
<th:block th:with="str='hello <strong>thymeleaf</strong>'">
<p>[[${str}]]</p>
<p>[(${str})]</p>
</th:block>
有时我们需要在JavaScript代码中访问session中的
值,也可以用行内表达式
<!--取出session中的user-->
<script th:inline="javascript">
var user = [[${session.user}]]
</script>
thymeleaf的常用语法就这些,还有一些内置对象的使用,遇到的话可以查阅文档。
原文:https://www.cnblogs.com/wotoufahaiduo/p/11354142.html