• 综合练习:热点事件排行榜(页面使用的是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

    1. <!--引入thymeleaf-->
    2. <dependency>
    3. <groupId>org.springframework.boot</groupId>
    4. <artifactId>spring-boot-starter-thymeleaf</artifactId>
    5. </dependency>

    2.自动配置了Thymeleaf

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

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

    3.编写Controller

    1. import com.msds.bean.Student;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.ui.Model;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import java.util.ArrayList;
    6. import java.util.List;
    7. import java.util.Map;
    8. @Controller
    9. public class HelloController {
    10. //查出一些数据,在页面显示
    11. @RequestMapping(value = "/cg")
    12. public String success(Map<String,Object> map, Model model){
    13. map.put("hello","你好SpringBoot");
    14. map.put("html","

      html

      "
      );
    15. String[] str={"张三","李四","王五"};
    16. map.put("arr",str);
    17. List<Student> list=new ArrayList<Student>();
    18. Student stu1=new Student("欢欢",7,'女');
    19. Student stu2=new Student("贝贝",8,'女');
    20. Student stu3=new Student("小黑",10,'男');
    21. list.add(stu1);
    22. list.add(stu2);
    23. list.add(stu3);
    24. map.put("stu",list);
    25. model.addAttribute("msg","向model中存了些数据");
    26. //classpath:/templates/success.html
    27. return "success";
    28. }
    29. }

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

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

     

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

     使用thymeleaf语法;

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

    5.启动项目,进行访问

    配置热部署

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

    1. <!--配置热部署-->
    2. <dependency>
    3. <groupId>org.springframework.boot</groupId>
    4. <artifactId>spring-boot-devtools</artifactId>
    5. </dependency>

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

    thymeleaf常用语法

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

    变量

    1. 先新建一个实体类User
    2. public class User {
    3. private Stri
  • 相关阅读:
    RocketMQ专题02
    机械工程基础笔记整理
    Spring框架及创建Spring项目
    apifox 接口性能压测
    最简单的springboot整合websocket方式
    GPDB7-新特性-角色创建
    接口自动化中如何完成接口加密与解密?
    大众点评字体反爬解析
    《向量数据库指南》——向量数据库和关系型数据库的区别?
    【Python进阶-PyQt5】00搭建PyQt5环境
  • 原文地址:https://blog.csdn.net/weixin_47541976/article/details/126776350