0
点赞
收藏
分享

微信扫一扫

Thymeleaf -模板引擎

亿奇学 2021-09-21 阅读 159

允许处理的模板

  • xml
  • html5

SpringBoot整合Thymeleaf

  • 创建springboot项目
  • 添加springboot-web起步依赖
  • 添加springboot-thymeleaf起步依赖
  • 编写html使用thymeleaf语法获取变量对应后台传递的值
  • 编写controller设置变量的值到model中

pom.xml 添加依赖

   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>
    <dependencies>
        <!--web起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--thymeleaf配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>

tips:

  • thymeleaf的缓存设置
spring:
  thymeleaf:
    cache: false
  • 创建html
    在resources中创建templates目录,在templates目录创建 demo.html

  • 声明使用thymeleaf标签

<html xmlns:th="http://www.thymeleaf.org">

基本语法

(1)th:action 定义后台控制器路径

  • html
<form id="login-form" th:action="@{/test/hello}">
    <button>提交</button>
</form>

表示提交的请求地址为/test/hello

(2)th:each 对象遍历(list,map,数组)

  • controller
@RequestMapping("/hello")
public String hello(Model model){
    //集合数据
    List<User> users = new ArrayList<User>();
    users.add(new User(1,"张三","深圳"));
    users.add(new User(2,"李四","北京"));
    users.add(new User(3,"王五","武汉"));
    model.addAttribute("users",users);
    return "demo";
}
  • html
<table>
    <tr th:each="user,userStat:${users}">
        <td>下标:<span th:text="${userStat.index}"></span></td>
        <td th:text="${user.id}"></td>
        <td th:text="${user.name}"></td>
        <td th:text="${user.address}"></td>
    </tr>
</table>

(3)Map输出

  • controller
//Map定义
Map<String,Object> dataMap = new HashMap<String,Object>();
dataMap.put("No","123");
dataMap.put("address","深圳");
model.addAttribute("dataMap",dataMap);
  • html
<div th:each="map,mapStat:${dataMap}">
    <div th:text="${map}"></div>
    key:<span th:text="${mapStat.current.key}"></span><br/>
    value:<span th:text="${mapStat.current.value}"></span><br/>
</div>

(4)数组输出

  • controller
//存储一个数组
String[] names = {"张三","李四","王五"};
model.addAttribute("names",names);
  • html
<div th:each="nm,nmStat:${names}">
    <span th:text="${nmStat.count}"></span>
    <span th:text="${nm}"></span>
</div>

(5)Date 输出

  • controller
//日期
model.addAttribute("now",new Date());
  • html
<div>
    <span th:text="${#dates.format(now,'yyyy-MM-dd hh:ss:mm')}"></span>
</div>

(6)th:if 条件

  • controller
//if条件
model.addAttribute("age",22);
  • html
<div>
    <span th:if="${(age>=18)}">成年人</span>
    // 不成立
    <span th:unless="${(age<18)}">成年人</span>
</div>

(7)th:fragment 可以定义一个独立的模块

  • footer.html
<body>
<div id="C" th:fragment="copy" > 关于我们<br/></div>
</body>

(8)th:include 可以直接引入 th:fragment

  • dome.html
<div id="A" th:include="footer::copy"></div>
举报

相关推荐

0 条评论