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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • c#程序中的AssemblyInfo.cs

    c#程序中的AssemblyInfo.cs在asp.net中有一个配置文件AssemblyInfo.cs主要用来设定生成的有关程序集的常规信息dll文件的一些参数,下面是默认的AssemblyInfo.cs文件的内容具体介绍//是否符合公共语言规范(CLS)[assembly:CLSCompliant(true)]//控制程序集中所有类型对COM的可访问性[assembly:ComVisible(false)]//代码的作者和这…

    2022年7月13日
  • 行测题库图形推理_2021年考公务员

    行测题库图形推理_2021年考公务员事业单位考试中,判断推理是占据一定的地位的,其中图形推理是大家经常见到的。然而,有的考生会觉得这类题目有些难,像“相亲”一样,都是看眼缘。实则不然,图形推理有它自己的规律,大家需要根据图形特征去识别该题对应的规律。比如图形相似我们可以从位置变化或者是组合叠加角度上去考虑。如果相似度很高我们更建议大家考虑位置变化,那今天中公教育老师带大家来看一下何为位置变化。1.从所给的四个选项中,选择最合适的一个…

    2022年9月10日
  • luogu1146

    luogu1146#include#definefr(i,a,b)for(inti=(a),i_end=(b);i<=i_end;i++)#definefrd(i,a,b)for(inti=(a),i_end=(b);i>=i_end;i–)#definelllonglong#definepri(x)printf(“%d”,x)#definemes(x

    2022年5月16日
  • JavaScript中的document.cookie的使用

    JavaScript中的document.cookie的使用 我们已经知道,在document对象中有一个cookie属性。但是Cookie又是什么?“某些Web站点在您的硬盘上用很小的文本文件存储了一些信息,这些文件就称为Cookie。”——MSIE帮助。一般来说,Cookies是CGI或类似,比HTML高级的文件、程序等创建的,但是javascript也提供了对Cookies的很全面的访问权利。  我们先要学

    2022年7月27日
  • html页面缩小导航栏隐藏,html – 导航栏缩放问题[通俗易懂]

    html页面缩小导航栏隐藏,html – 导航栏缩放问题[通俗易懂]我有一个问题,我的导航栏似乎与CSS中的.container缩放.现在,我是一个新手,但我已经尝试搞乱CSS中的值,但无济于事.这是HTML和CSS的代码:*{margin:0px;padding:0px;}body{font-family:verdana;background-image:url(images/bg2.jpg);max-width:100%;max-height:…

    2022年5月28日
  • AttributeCollection类与Attributes.Add方法的使用[通俗易懂]

    AttributeCollection类与Attributes.Add方法的使用[通俗易懂]控件属性集合:AttributeCollection类AttitudeCollection隶属于namespaceSystem.Web.UI命名空间。AttributeCollection类属于集合类,具有其他集合所共有的一些属性。AttributeCollection类用来封装服务器控件的所有属性,可实现对服务器属性集合的添加和删除。控件的属性包括颜色、样式、名称等,这些属性都可以通过At

    2022年9月26日

发表回复

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

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