综合练习:热点事件排行榜(页面使用的是thymeleaf)

Thymeleaf是用来开发Web和独立环境项目的服务器端的Java模版引擎

Spring官方支持的服务的渲染模板中,并不包含jsp。而是Thymeleaf和Freemarker等,而Thymeleaf与SpringMVC的视图技术,及SpringBoot的自动化配置集成非常完美,几乎没有任何成本,你只用关注Thymeleaf的语法即可。

Thymeleaf的特点 

1.动静结合:Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。

2.开箱即用:它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、改jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。

3.多方言支持:Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。

4.与SpringBoot完美整合:SpringBoot提供了Thymeleaf的默认配置,并且为Thymeleaf设置了视图解析器,我们可以像以前操作jsp一样来操作Thymeleaf。代码几乎没有任何区别,就是在模板语法上有区别。

SpringBoot与Thymeleaf整合

1.在pom中引入Thymeleaf


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


2.自动配置了Thymeleaf

SpringBoot中已经自动给我们默认分配了模版的前缀和后缀,我们只需要按部就班的将模版丢进去即可

 只要我们把HTML页面放在classpath:/templates/,thymeleaf就能自动渲染;

3.编写Controller

import com.msds.bean.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Controller
public class HelloController {
    //查出一些数据,在页面显示
    @RequestMapping(value = "/cg")
    public String success(Map<String,Object> map, Model model){
        map.put("hello","你好SpringBoot");
        map.put("html","<h1>html</h1>");
        String[] str={"张三","李四","王五"};
        map.put("arr",str);
        List<Student> list=new ArrayList<Student>();
        Student stu1=new Student("欢欢",7,'女');
        Student stu2=new Student("贝贝",8,'女');
        Student stu3=new Student("小黑",10,'男');
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);
        map.put("stu",list);
        model.addAttribute("msg","向model中存了些数据");
        //classpath:/templates/success.html
        return "success";
    }
}

4.将返回的对应模板添加到默认的位置下:在resources/templates下创建success.html

在html页面导入thymeleaf的名称空间——(导入是为了可以有快捷提示,也可以选择不导入)

 

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

 使用thymeleaf语法;


<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>success</title>
</head>
<body>
    <h1>成功</h1>
    <!--th:text 将div里面的文本内容设置为${hello} 要显示的值-->
    <h3 th:text="${hello}"></h3>
    <!--这句话在后台请求运行的时候并不会显示,会直接替换为后台的数据,后台中没有key为hello对应的value值,那就会把${}中的数据给显示出来-->
    <h3 th:text="${hello}">这是显示欢迎信息</h3>
    <!--会将原本id的值替换为th:id中取出的值-->
    <a id="aid" th:id="${hello}">超链接</a>
    <hr/>
    <!--th:each 每次遍历都会生成当前这个标签 3个h4-->
    <h4 th:text="${us}" th:each="us:${arr}"></h4>
    <hr/>
    <!--行内写法-->
    <h4>
        <span th:each="us:${arr}">[[${us}]] </span>
    </h4>
    <hr/>
    <h4 th:text="${xs.name+'、'+xs.age+'、'+xs.sex}" th:each="xs:${stu}"></h4>
    <hr/>
    <h4>
        <span th:each="xs:${stu}">[[${xs.name}]]、[[${xs.age}]]、[[${xs.sex}]] <br/></span>
    </h4>
    <hr/>
    <p th:text="${msg}"></p>
</body>
</html>


5.启动项目,进行访问

配置热部署

1.在项目的pom文件中添加如下依赖

    <!--配置热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency> 

2.再次修改了java代码或html页面,只需要按Ctrl+F9键,就可有重新访问(指的是在IDEA软件中)

thymeleaf常用语法

thymeleaf主要作用是把后台数据渲染到html中

变量

先新建一个实体类User
public class User {
    private Stri
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Qinꦿ.๓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值