Spring Boot实战第七章-SpringBoot Web开发-Thymeleaf模板引擎

Spring Boot实战第七章-SpringBoot Web开发-Thymeleaf模板引擎

本章介绍了Spring Boot Web开发的一些内容,涉及了很多前端的东西,简单了解下前端的东西就好,不必深究,遇到有开发前端的需求时可以看下官方文档,很快可以入手。重点放在web和tomcat的配置上面。

本篇文章讲的是Thymeleaf引擎,是Spring Boot比较推荐的,它提供了完美的Spring MVC的支持。

1.基本理解

Thymeleaf是一个java类库,它是一个xml/xhtml/html5模板引擎,可以作为MVC的view层。还提供了额外的模块与Spring MVC集成,可以完全代替JSP。其实最好的地方是后缀可以为html,能够直接用浏览器渲染,在用springboot打成jar包的时候也是可以直接用的,不像jsp用内置tomcat运行的时候还得需要其他的东西。

2.如何在spring boot中引入使用

(1)引入依赖

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

在这个依赖中已经包含了spring-boot-starter-web,所以可以不用再引入spring-boot-starter-web了

(2)配置视图解析器

  由于spring boot的自动配置,文件放在默认的位置就好,我们可以看下源码,配置的前缀是spring.thymeleaf,

默认路径是classpath:/templates/,后缀是html

  Spring Boot实战第七章-SpringBoot Web开发-Thymeleaf模板引擎

那么,我们可以在配置文件里配置参数,当然,默认的就好,可以配置下其他的参数,比如:

#开发的时候可以关闭缓存

spring.thymeleaf.cache=false

3.基本语法

(1).引入Thymeleaf

<html xmlns:th=”http://www.thymeleaf.org”>

通过xmlns:th=”http://www.thymeleaf.org”命名空间,将镜头页面转换成动态视图,需要动态处理的元素将使用th:为前缀

<script th:src=”@{js/jquery-3.3.1.min.js}”></script>

通过@{}引入web静态资源

(2).访问model中的数据

${}

例如:<span th:text=”${singlePerson.name}”></span>

需要动态处理的前面加上th:

(3).model中是数据迭代

th:each=”…”

例如:th:each=”person:${people}”,person作为迭代元素来使用

(4).数据判断

Thymeleaf支持>、<、>=、<=、==、!=作为比较条件,同时也支持将SpringEL表达式应用于条件中

例如:th:if=”${not #lists.isEmpty(people)}

(5).JavaScript访问model

通过th:inline=”javascript”添加到script标签,这样JavaScript可以访问model中的属性

通过”[[${}]]”获取实际的值

还有一种需要在html中获取model中的属性,格式例如:th:οnclick=”‘getName(\”+${person.name}+’\’)'”

4.实战内容

(1).JavaBean

public class Person {
    private String name;
    private Integer age;

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

(2).演示页面

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>spring boot web index</title>
    <link th:href="@{boostrap/css/bootstrap.min.css}" rel="stylesheet">
    <link th:href="@{boostrap/css/bootstrap-theme.min.css}" rel="stylesheet">
</head>
<body>
    <div class="panel panel-primary">
        <div class="panel-heading">
           <h3 class="panel-title">访问model</h3>
        </div>
        <div class="panel-body">
            <span th:text="${singlePerson.name}"></span>
        </div>
    </div>
    <div th:if="${not #lists.isEmpty(people)}">
       <div class="panel panel-primary">
           <div class="panel-heading">
               <h3 class="panel-title">列表</h3>
           </div>
           <div class="panel-body">
              <ul class="list-group">
                 <li class="list-group-item" th:each="person:${people}">
                      <span th:text="${person.name}"></span>
                      <span th:text="${person.age}"></span>
                     <button class="btn btn-primary" th:οnclick="'getName(\''+${person.name}+'\')'">获得名字</button>
                 </li>
              </ul>
           </div>
       </div>
    </div>
    <script th:src="@{js/jquery-3.3.1.min.js}" type="text/javascript"></script>
    <script th:src="@{bootstrap/js/bootstrap.min.js}" type="text/javascript"></script>
    <script th:inline="javascript">
     var single=[[${singlePerson}]];
     console.log("single.name:"+single.name+"single.age:"+single.age);
     function getName(name) {
         alert(name);
     }
</script>
</body>
</html>

(3).数据准备

@GetMapping("/")
    public String index(Model model){
        Person single=new Person("小明",11);
        List<Person> people=new ArrayList<>();
        Person person1=new Person("二狗子",18);
        Person person2=new Person("二摇子",19);
        Person person3=new Person("二大爷",66);
        people.add(person1);
        people.add(person2);
        people.add(person3);
        model.addAttribute("singlePerson",single);
        model.addAttribute("people",people);
        return "index";
    }

(4).运行结果

Spring Boot实战第七章-SpringBoot Web开发-Thymeleaf模板引擎

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/100302.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 随机梯度下降算法过程详细解读_python 排序算法

    随机梯度下降算法过程详细解读_python 排序算法梯度下降算法梯度下降,依照所给数据,判断函数,随机给一个初值w,之后通过不断更改,一步步接近原函数的方法。更改的过程也就是根据梯度不断修改w的过程。以简单的一元函数为例原始数据为x_data=[1.0,2.0,3.0]y_data=[2.0,4.0,6.0]因此我们设置函数为对于该函数,我们的w是未知的,因此如何根据xy的数据,获取到正确的w值就是梯度下降…

    2025年10月19日
    4
  • matlab fopen fread_matlab中prctile函数

    matlab fopen fread_matlab中prctile函数matlab中length函数length(x)在matlab中是什么意思?小编能记住你的一点一滴,你却忘了小编的一丝一毫。iflength(h)>1||h<0||h>2*breturn是什么意思?爱的仓促,就像行走于沙漠,风一场就没有了后路。length(x0)为数列的长度,即它里面有多少个元素。n=length(A):如果A为非空数组,返回行数和列数两者之间数…

    2025年11月6日
    4
  • Laravel报错Call to undefined function Illuminate\Encryption\openssl_cipher_iv_length()

    Laravel报错Call to undefined function Illuminate\Encryption\openssl_cipher_iv_length()

    2021年10月20日
    43
  • es6数组常用函数方法

    es6数组常用函数方法//一,Array.from()将一组类似数组的对象转换为数组letsetx=newSet([1,2,3,4]);letarr=Array.from(setx);console.log(arr);//结果:[1,2,3,4]//二,Array.of(值1,值2,值3……)将一组值转换位数组leta=’12344′;letb=’2345′;le…

    2022年5月7日
    49
  • 数据归一化小结

    数据归一化小结在各种模型训练,特征选择相关的算法中,大量涉及到数据归一化的问题。比如最常见的情况是计算距离,如果不同维度之间的取值范围不一样,比如feature1的取值范围是[100,200],feature2的取值范围是[1,2],如果数据不做归一化处理,会造成feature1在距离计算中占压倒性的优势,feature2完全体现不出来作用。而数据做归一化处理以后,会让各个不同特征对距离计算的贡献大致相同,从而避

    2022年6月23日
    26
  • c++酒店管理系统课程设计_酒店管理系统毕业设计

    c++酒店管理系统课程设计_酒店管理系统毕业设计课程设计-C#酒店管理系统1.主要功能:客户的入住管理、预定管理、客房管理、用户管理、收银结算、补交押金等等。数据库用的是Access。2.效果截图:登录页面主页面主要功能3.项目文件截图:想要源码加我QQ1352677818…

    2022年9月24日
    8

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号