spring boot2.0 集成thymeleaf

Thymeleaf介绍

  • (1)什么事模板技术?
    由模板引擎数据与模板页面结合在一起,形成页面
    在这里插入图片描述
  • (2)什么是thymeleaf?
    spring boot并不推荐使用jsp,但是支持一些模板引擎技术,如:Freemarker,Thymleaf,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;一般我们无需进行修改,默认即可。
    在这里插入图片描述

测试

  • (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>
Domain下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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值