spring boot学习六

Thymeleaf模板引擎在Spring Boot中的使用

之前学习的引入资源没太弄懂,回过来看,到底什么是Thymeleaf?

1,什么是Thymeleaf?

(1) Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎。

(2)类似JSP,Velocity,FreeMaker等, 它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用的模板引擎。

(3)与其它模板引擎相比, Thymeleaf最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个Web应用。

2, 导入thymeleaf模块相关的starter

<!-- thymeleaf模板引擎-->
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

在这里插入图片描述
3,thymeleaf相关demo

已经导入了thymeleaf相关的starter,下面创建一个java:

package com.guigu.springboot.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @RequestMapping("/success")
    public String success(ModelMap modelMap) {
        String str = "hello, how are u?";
        modelMap.addAttribute("hello", str);
        return "success";
    }
}

在这里插入图片描述
我们给ModelMap绑定一个属性上,属性名为hello,属性值为字符串:hello, how are u?

然后直接success字符串,thymeleaf就会自动加载calsspath:/templates/success.html

下面在templates文件夹下创建success.htm,

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>SUCCESS</title>
</head>
<body>
<div th:text="${hello}"></div>
</body>
</html>

在这里插入图片描述
导入thymeleaf的starter之后,穿件html时会在顶部自动加入thymeleaf的官网,这样我们在html里面写thymeleaf时会有语法提醒。
在div中加上了 th:text="${hello}", 这句的含义是,用这个值替换div的文本的值。
4,启动项目,然后访问success方法,就会输出我们在hello上绑定的值:hello, how are u?
在这里插入图片描述
thymeleaf使用起来,模式都一样,后台传值, html页面利用thymeleaf语法进行展示。
理解学习一点是一点,fighting!!!!!
学习资源:http://www.chilangedu.com/sectionq/1489582623/0FE7879F04947075

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值