thymeleaf--简单使用

环境:spring-boot

作用之一:jsp有jstl标签库。html就可以用thymeleaf完成数据显示,以及交互。

使用

1.添加依赖

<properties>
        <!-- thymeleaf2   layout1-->
        <thymeleaf-layout-dialect.version>2.4.1</thymeleaf-layout-dialect.version>
</properties>

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

2.在templates文件夹下新建.html文件

在html的头中引入:

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

3.使用语法

controller:

package com.org.controller;

import com.org.pojo.dog;
import com.org.pojo.person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class thymeleafController {

    @RequestMapping("testyh")
    public String testyh(HttpServletRequest request){
        request.setAttribute("name","admin");
        ArrayList<Object> objects = new ArrayList<>();
        objects.add("admin");
        objects.add(18);
        objects.add("男");
        request.setAttribute("list", objects);
        request.setAttribute("p", new person("admin"));
        return "thymeleaf";
    }
}

访问:http://localhost/testyh

thymeleaf.html内容:(我新建的html)

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
[[(${name})]]
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/yangzhuxian/p/12787737.html