SpringBoot开发——整合Thymeleaf

一、pom.xml

  <!-- 继承父包 -->
  <parent>
    <artifactId>spring-boot-starter-parent</artifactId>
    <groupId>org.springframework.boot</groupId>
    <version>2.3.1.RELEASE</version>
  </parent>

  <dependencies>
    <!-- web -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- lombok -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
    </dependency>

    <!-- 整合Thymeleaf -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

二、application.yml

spring:
  # thymeleaf视图解析器
  thymeleaf:
    prefix: classpath:/templates/
    suffix: .html
    mode: HTML5
    encoding: utf-8

三、Handler

@Controller
@RequestMapping("/test")
public class TestHandler {
    @GetMapping("/test1")
    public String test1(Model model){
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student(1, "张三"));
        students.add(new Student(2, "李四"));
        model.addAttribute("list", students);
        return "test";
    }
}

四、test.html

放置于templates文件夹中,次文件夹中的资源无法再浏览器直接访问,static文件夹中的文件可以直接访问

  • 引入标签库<html xmlns:th="http://www.thymeleaf.org">
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <table>
        <tr>
            <th>id</th>
            <th>姓名</th>
        </tr>
        <tr th:each="student:${list}">
            <td th:text="${student.id}"></td>
            <td th:text="${student.name}"></td>
        </tr>

    </table>
</body>
</html>

五、Thymeleaf 常用语法

  • 赋值和拼接

    	<p th:text="${list}"></p>
        <p th:text="'名字是:'+${list}"></p>
        <p th:text="|名字是:${list}|"></p>
    
  • 条件判断 if/unless (成立时/不成立时)

    	<p th:if="${list==true}" th:text="条件成立"></p>
        <p th:unless="${list==true}" th:text="条件不成立"></p>
    
  • 循环

    	<tr th:each="student:${list}">
               <td th:text="${student.id}"></td>
               <td th:text="${student.name}"></td>
        </tr>
    
  • href拼接

    	<a th:href="@{http://localhost:8080/test/{na}(na=${name})}">跳转</a>
    
  • switch

    	<div th:switch="${gender}">
            <p th:case=""></p>
            <p th:case=""></p>
            <p th:case="*">未知</p>
        </div>
    
  • 基本对象
    #ctx :上下文对象
    #vars :上下文变量
    #locale :区域对象
    #request :HttpServletRequest 对象
    #response :HttpServletResponse 对象
    #session :HttpSession 对象
    #servletContext :ServletContext 对象

    <p th:text="${#request.getAttribute('request')}"></p>
    
  • 内嵌对象
    可以直接通过 # 访问。
    1、dates:java.util.Date 的功能方法
    2、calendars:java.util.Calendar 的功能方法
    3、numbers:格式化数字
    4、strings:java.lang.String 的功能方法
    5、objects:Object 的功能方法
    6、bools:对布尔求值的方法
    7、arrays:操作数组的功能方法
    8、lists:操作集合的功能方法
    9、sets:操作集合的功能方法
    10、maps:操作集合的功能方法

    <!-- 格式化时间 -->
    <p th:text="${#dates.format(date,'yyyy-MM-dd HH:mm:sss')}"></p>
    <!-- 创建当前时间,精确到天 -->
    <p th:text="${#dates.createToday()}"></p>
    <!-- 创建当前时间,精确到秒 -->
    <p th:text="${#dates.createNow()}"></p>
    <!-- 判断是否为空 -->
    <p th:text="${#strings.isEmpty(name)}"></p>
    <!-- 判断List是否为空 -->
    <p th:text="${#lists.isEmpty(users)}"></p>
    <!-- 输出字符串长度 -->
    <p th:text="${#strings.length(name)}"></p>
    <!-- 拼接字符串 -->
    <p th:text="${#strings.concat(name,name,name)}"></p>
    <!-- 创建自定义字符串 -->
    <p th:text="${#strings.randomAlphanumeric(count)}"></p>
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值