thymeleaf例子
来源:互联网 发布:日本文学 知乎 编辑:程序博客网 时间:2024/06/04 19:42
今天第一次使用thymeleaf模板,确实使用起来比较方便,thymeleaf是一个支持html原型的自然引擎,Thymeleaf提供了一个用于整合Spring MVC的可选模块。关于thymeleaf的详细语法:点击打开链接</span>这篇文章介绍的很仔细了,在这里我就不详细介绍了,现在看看这里的例子,我是用了一个spring-boot中的一个项目spring-boot-sample-ui来讲解的,有兴趣的朋友也可以去spring的官网下载这个项目来看看,下面我们一起来看看thymeleaf怎么使用的。
首先我们在使用thymeleaf之前必须引入其命名空间:
<span style="font-size:18px;"><html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout"></span></span></span>
我们来看看这个模板文件:
<span style="font-size:18px;"><!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout"> <head> <title th:text="${title}">Layout</title> <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}" href="../../css/bootstrap.min.css"/> </head> <body> <div class="container"> <div class="navbar"> <div class="navbar-inner"> <a class="brand" href="https://github.com/ultraq/thymeleaf-layout-dialect"> Thymeleaf - Layout </a> <ul class="nav"> <li> <a th:href="@{/}" href="messages.html"> Messages </a> </li> </ul> </div> </div> <h1 layout:fragment="header" th:text="${title}">Layout</h1> <div layout:fragment="content"> Fake content </div> <footer layout:fragment="footer" th:utext="${copyright}">Copyright © 2014 XXX INC.</footer> </div> </body></html></span></span>我们来分析一下上面的这段代码<title th:text="${title}">Layout</title>,由于我们在制作网页的时候每个页面的title都是不一样的,这样我们在用th:text="${title}"根据每个页面来设置title的值,在spring MVC 中controler层中我们是通过model.setAttribute("title","首页");比如这里设置一个首页,视图层和Controller层传数据是通过model对象在操作,这样我们在页面布局的时候,每个页面的title值是根据Controller层传来控制的,我们在增加消息的时候,增加页面的title应该为“添加消息”,可能这里看不thymeleaf的好处。
<span style="font-size:18px;"><link rel="stylesheet" th:href="@{/css/bootstrap.min.css}" href="../../css/bootstrap.min.css"/></span></span>这里是固定用法,是css的引入。固定用法,其实都都是th:开头的,这个是thymeleaf规定的用法,顺便看看js的内部用法:内部的js是如下的这种用法:
<scriptth:inline="javascript">
/*<![CDATA[*/
...
var username = /*[[${sesion.user.name}]]*/ 'Sebastian';
...
/*]]>*/
</script>
<span style="font-size:18px;"> <div layout:fragment="content"> Fake content这里面的内容是可以变化的 </div></span></span>这里是在模板文件中定义了一个div模板,相当于我们Java中的父类,我们在父模板放一些假数据,当我们子模板需要改变这里面的内容的时候只需要在子页面重写一个<div layout:fragment="content">子类的页面内容</div>
<span style="font-size:18px;"> <footer layout:fragment="footer" th:utext="${copyright}">Copyright © 2014 XXX INC.</footer></span></span>比如我们在常常在做页面的底部的时候,大多都是一些版权的问题,这时候,我们只需要在模板中定义好,我们的每个子页面就不会重新写代码这段底部的代码,以前错了一个地方,我们经常跑到每个页面去修改,现在只要修改模板文件中的内容就可以了,所有的页面的底部就直接变过来了,这就是模板的好处。
总结:我们在使用thymeleaf的时候,html标签一定要结束,我们可以使用这个工具www.jsoup.org直接将我们的标签变为HTML5的规范,比如没有结束的标签,这个工具自动帮我们加结束标签。后面更新Spring MVC和thymeleaf的嵌套使用。
0 0
- thymeleaf例子
- Thymeleaf
- thymeleaf
- thymeleaf
- Thymeleaf
- Thymeleaf
- Thymeleaf
- Thymeleaf
- Thymeleaf
- Thymeleaf
- Thymeleaf
- java鬼混笔记:springboot之thymeleaf 1:简单的thymeleaf例子
- Spring MVC与thymeleaf模版引擎集成的简单例子
- Thymeleaf 错误 以及thymeleafMVC配置,前端代码例子
- Thymeleaf教程 (十一) 一些商店的过多的模板页面例子
- Thymeleaf教程 (十一) 一些商店的过多的模板页面例子
- thymeleaf api
- Thymeleaf基本知识
- 黑马程序员——Java基本知识
- 梦想与行动:仰望星空,脚踏实地
- Git:一分钟学会简单使用
- Linux企业级开发技术(1)——epoll企业级开发之简介
- 北斗的投资哲学、投资理念及投资策略
- thymeleaf例子
- HDU 2845 Beans
- IOS8系统下,APP开发的不同,及碰到的问题收集
- Maven配置本地库加载ojdbc14-10.2.0.4.0.jar文件
- Linux 静态库&动态库调用
- 【Java.JMS】点对点通信——Queue
- Android 3.0 r1中文API文档(103) —— InputMethodManager [输入法]
- android完全退出应用程序
- PHP格式化输出json数据