ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SpringBoot2.0 集成thymeleaf

2020-12-14 16:57:50  阅读:320  来源: 互联网

标签:集成 SpringBoot2.0 list Thymeleaf thymeleaf html jack th 模板


Thymeleaf介绍

(1)什么是模板技术?

由模板引擎将数据与模板页面合在一起,形成页面
在这里插入图片描述

(2)什么是thymeleaf?

SpringBoot并不推荐使用jsp,但是支持一些模板引擎技术,如:Freemarker,Thymeleaf,Mustache

(3)为什么选择Thymeleaf

可以完全替代jsp

(4)有什么特点

》动静结合,直接访问或者通过服务器访问
浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行
当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示
》开箱即用:它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、改jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。
》多方言支持:Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。
》与SpringBoot完美整合,SpringBoot提供了Thymeleaf的默认配置,并且为Thymeleaf设置了视图解析器,我们可以像以前操作jsp一样来操作Thymeleaf。代码几乎没有任何区别,就是在模板语法上有区别。

Thymeleaf集成

(1)引入启动器

(2)SpringBoot会自动为Thymeleaf注册一个视图解析器:ThymeleafViewResolver

默认前缀:classpath:/templates/
默认后缀:.html
如果我们返回视图:users,会指向到 classpath:/templates/users.html;一般我们无需进行修改,默认即可。
在这里插入图片描述
Thymeleaf集成测试
(3)Controller提供数据
使用ModelMap将数据与页面合在一起

    @RequestMapping(path="/test01",method = {RequestMethod.GET})
    public String test01(ModelMap modelMap){ //带数据建议大家使用ModelMap
        //name jack
        modelMap.addAttribute("name","jack");
        return "person-list"; //classpath:/template/person-list.html
    }

(4)编写html模板

渲染模型中的数据

注意:把html 的名称空间,改成:xmlns:th="http://www.thymeleaf.org"会有语法提示

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" >
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:text="${name}">杰克</div>
</body>
</html>

显示列表数据

Person

@Data
public class Person {
    private String username;
    private String password;
}

PersonController

@Controller
public class PersonController {

    @RequestMapping(path="/test01",method = {RequestMethod.GET})
    public String test01(ModelMap modelMap){ //带数据建议大家使用ModelMap
        //name jack
        modelMap.addAttribute("name","jack");

       //准备三个人的数据放到页面
        List<Person> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Person p = new Person();
            p.setUsername("jack"+i);
            p.setPassword("123456");
            list.add(p);
        }
        //添加数据
        modelMap.addAttribute("list",list);
        return "person-list"; //classpath:/template/person-list.html
    }
}

person-list.html

<table>
    <tr>
        <td>账号</td>
        <td>密码</td>
    </tr>

    <tr th:each="person: ${list}">
        <td th:text="${person.username}">jack</td>
        <td th:text="${person.password}">123456</td>
    </tr>
</table>

Thymeleaf入门案例说明

(5)
${} :这个类似与el表达式,但其实是ognl的语法,比el表达式更加强大
th-指令:th-是利用了Html5中的自定义属性来实现的。
如果不支持H5,可以用data-th-来代替
th:each:类似于c:foreach 遍历集合,但是语法更加简洁
th:text:声明标签中的文本
例如1,如果user.id有值,会覆盖默认的1
如果没有值,则会显示td中默认的1。
这正是thymeleaf能够动静结合的原因,模板解析失败不影响页面的显示效果,因为会显示默认值!
th-指令语法(查询用)
在这里插入图片描述

表达式

(1)Simple expressions:(表达式语法)
Variable Expressions: ${…}:获取变量值;OGNL;
1)、获取对象的属性、调用方法
2)、使用内置的基本对象:
#ctx : the context object.

标签:集成,SpringBoot2.0,list,Thymeleaf,thymeleaf,html,jack,th,模板
来源: https://blog.csdn.net/qq_42150520/article/details/111177971

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有