详解SpringBoot+Thymeleaf 基于HTML5的现代模板引擎
序言:
thymeleaf 是java服务端的模板引擎,与传统的jsp不同,前者可以使用浏览器直接打开,因为可以忽略掉拓展属性,相当于打开原生页面,给前端人员也带来一定的便利。如果你已经厌倦了jsp+jstl的组合,thymeleaf或许是个不错的选择!本工程传送门:springboot-web-thymeleaf
开始使用
1.引入依赖
springboot默认提供了thymeleaf的starter,只需简单引入依赖即可。
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-thymeleaf</artifactid> </dependency>
目前默认版本是2.1,如果想升级版本到3.0,可以这样修改。
<properties> <thymeleaf.version>3.0.7.release</thymeleaf.version> <thymeleaf-layout-dialect.version>2.0.0</thymeleaf-layout-dialect.version> </properties>
为了方便开发,项目案例采用了热部署工具dev-tools ,这样我们在修改页面之后,idea会自动加载,从而达到实现热更新的效果。
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-devtools</artifactid> <scope>runtime</scope> </dependency>
注:由于idea默认关闭了热部署,需要做一些设置才能使其生效。解决方法:首先按住ctrl+shift+alt+/ 然后进入 registry ,然后勾选compiler.automake.allow.when.app.running 即可。另外,build->compiler 也要勾选上build project automatically .
2. 添加相关配置
thymeleaf默认开启了页面缓存,在开发的时候,应该关闭缓存。此外,通常我们还会指定页面的存放路径。(默认是classpath:/templates/)
application.yml 配置如下: spring: thymeleaf: cache: false #关闭缓存 prefix: classpath:/views/ #添加路径前缀
3.编写html
编写thymeleaf和书写html5页面没有什么不同,最大的转变就是使用拓展属性(th:xx)去跟服务端进行数据交互,保留原始页面风格,也是thymeleaf的推崇的风格。例如下面这个简单的案例,启动项目,我们发现页面跳转的是简书的连接,这一点也验证了thymeleaf覆盖原生页面数据的极佳能力。
页面代码:
<!doctype html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>thymeleaf</title> </head> <body> <h2>欢迎使用thymeleaf!!</h2> <a href="http://www.baidu.com" rel="external nofollow" th:href="${info}" rel="external nofollow" >戳我有惊喜</a> </body> </html>
后端代码:
@controller public class usercontroller { @getmapping("/") public string index(model model) { model.addattribute("info", "user/list"); return "index"; } @getmapping("/user") public string hehe(model model) { model.addattribute("user", new user(uuid.randomuuid().tostring(), "yizhiwazi", "20170928")); return "user"; } @getmapping("/user/list") public string userlist(model model) { list<user> userlist = new arraylist<>(); userlist.add(new user(uuid.randomuuid().tostring(), "yizhiwazi", "20170928")); userlist.add(new user(uuid.randomuuid().tostring(), "kumamon", "123456")); userlist.add(new user(uuid.randomuuid().tostring(), "admin", "admin")); model.addattribute("userlist", userlist); return "userlist"; } }
现在我们尝试回填一个表单,展示单个用户信息。
<!-- 使用th:object 搭配*{} 可以省略对象名 --> <form action="/" th:object="${user}" > <input type="hidden" name="userid" th:value="*{userid}" /> <input type="text" name="username" th:value="*{username}" /> <input type="text" name="password" th:value="*{password}" /> </form>
接下来,我们进入一个更复杂的案例,例如展示一个用户列表信息,不需要编写新的标签,就可以完成对批量用户的遍历。
<h2>用户列表</h2> <!--说明: th:each="obj,stat:${objects}" 分别代表单个实例,状态(可省略),待遍历对象--> <div th:each="user:${userlist}"> <input type="hidden" name="userid" th:value="${user.userid}"/> 用户姓名:<input type="text" name="password" th:value="${user.username}"/> 登录密码:<input type="text" name="username" th:value="${user.password}"/> </div>
好了,thymeleaf简单介绍到这里,更多详细说明,可阅读thymeleaf 官方指南 3.0
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 深入理解python中的atexit模块
下一篇: python 调用c语言函数的实例讲解