官网:https://www.thymeleaf.org/

点我跳 3.0文档

Thymeleaf 是适用于 Web 和独立环境的现代服务器端 Java 模板引擎,能够处理 HTML、XML、JavaScript、CSS 甚至纯文本。

Thymeleaf 的主要目标是提供一种优雅且高度可维护的模板创建方式。

正式接入

配置文件关闭缓存!

#Thymyleaf
# 关闭Thymeleaf缓存
spring.thymeleaf.cache=false
import com.xunliao.zhenliaowms.pojo.DisksInfomation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : zanglikun
 * @date : 2021/11/10 15:30
 * @Version: 1.0
 * @Desc : Thymeleaf启动类
 */
//@Controller
//@RequestMapping("/thy")
@Api(tags = "ThymeLeaf 模板")
public class ThymeleafController {

    /**
     * 说明:Thymeleaf 返回的内容是X  就是去找templates下面的 X.html
     * @return
     */
    @GetMapping("/thymeleafdemo")
    @ApiOperation("测试Thymeleaf")
    @ApiImplicitParam(name = "words",value = "你可以随便输入的内容")
    public String thymeleafDemo(Model model,String words){
        System.out.println("接受到的words是:"+words);
        model.addAttribute("diyword",words);

        // 对象数据
        SystemController systemController = new SystemController();
        ArrayList<DisksInfomation> diskInfo = systemController.getDiskInfo();
        model.addAttribute("keys",diskInfo);


        // map数据
        Map map = new HashMap();
        map.put("id","001");
        map.put("name","张三");
        map.put("age","15");
        map.put("sex","男");
        model.addAttribute("allmap",map);


        // 数组
        String[] shuzu = {"zhangsan","lisi","wangwu","zhaoliu"};
        model.addAttribute("allshuzu",shuzu);

        // 日期
        model.addAttribute("now",new Date());

        // 判断
        model.addAttribute("age",20);

        return "thymeleafdemo";
    }
}