jsp
到thymeleaf
,再到vue
,这是一个先后端逐渐分离的过程。jsp
的特色是页面中能够嵌套java代码,这显然很强大,但致使的结果是页面逻辑
(一些前端界面元素的显示隐藏,以及动态变化)和业务逻辑
(一些须要访问后台的操做,如数据库查询)经常纠缠在一块儿,代码混乱不堪,可维护性差,先后台人员职责分工不明确,责任难以追踪。到了thymeleaf
,状况好了不少,由于thymeleaf
支持html
界面,不管是否是在服务器环境下,界面都能展现,在界面上直接与后台交互的状况也少了不少。再到如今很火的vue
,才算实现了真正意义上的先后端分离,前端人员专一于前台界面,后端人员专一于后台接口。Springboot整合thymeleaf很简单,下面只介绍下几个步骤: 1.导入依赖javascript
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2.简单配置 application.propertiescss
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界面引入名称空间html
<!doctype html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> ...... </html>
thymeleaf语法都是html标签加th:*
的形式 <br/>前端
//输出字符串 thymeleaf <h2 th:text="'thymeleaf'"></h2>
//输出字符串 thymeleaf <h2 th:utext="'thymeleaf'"></h2>
th:text和th:utext的区别:<br/> th:utext中的html标签会正常翻译成html标签,而th:text的内容原样输出。以下:vue
<p th:text="'hello <b>thymeleaf</b>'"></p> //原样输出 <p th:utext="'hello <b>thymeleaf</b>'"></p> //thymeleaf加粗
输出包含空格的文本:<br/>java
<h2 th:text="'thymeleaf home page'"></h2> <h2 th:utext="|thymeleaf home page|"></h2>
全部th:*标签均可以用data-th-*替换:spring
<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>
<code>$</code>用于后端取值
后端要向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