【SpringBoot教程】SpringBoot Thymeleaf 基于HTML5的现代模板引擎

作者简介:大家好,我是撸代码的羊驼,前阿里巴巴架构师,现某互联网公司CTO

联系v:sulny_ann(17362204968),加我进群,大家一起学习,一起进步,一起对抗互联网寒冬

# 序言


Thymeleaf 是Java服务端的模板引擎,与传统的JSP不同,前者可以使用浏览器直接打开,因为可以忽略掉拓展属性,相当于打开原生页面,给前端人员也带来一定的便利。如果你已经厌倦了JSP+JSTL的组合,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 (或者双击Shift搜索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" th:href="${info}">戳我有惊喜</a></body></html>
后端代码:
@Controllerpublic 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简单介绍到这里

相关推荐

  1. SpringBoot整合FreeMarker模板引擎

    2023-12-11 11:44:02       57 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-11 11:44:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-11 11:44:02       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-11 11:44:02       82 阅读
  4. Python语言-面向对象

    2023-12-11 11:44:02       91 阅读

热门阅读

  1. EtherCAT主站程序代码详解

    2023-12-11 11:44:02       53 阅读
  2. 【前端设计模式】之外观模式

    2023-12-11 11:44:02       61 阅读
  3. mysql的行锁具体是怎么工作的

    2023-12-11 11:44:02       63 阅读
  4. leetcode周赛375 - 12 - 10

    2023-12-11 11:44:02       63 阅读
  5. redis底层数据结构之ziplist实现

    2023-12-11 11:44:02       60 阅读
  6. 如何利用jQuery来向一个元素中添加和移除CSS类?

    2023-12-11 11:44:02       60 阅读
  7. JWT的原理

    2023-12-11 11:44:02       49 阅读
  8. 尼科彻斯定理

    2023-12-11 11:44:02       59 阅读