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


相关推荐

  • latex中插入图片[通俗易懂]

    latex中插入图片[通俗易懂]latex排版之插入图片:(1)插入单个图片,图片格式为png\begin{figure}[h]\centering\includegraphics[weight=4cm,height=5cm]{3}\caption{这是一张图片,名字是3.png.}\end{figure}上面代码,第一行中[h]代表图片就在当前位置,有的时候latex排版的时候图片太大,而剩余的空间太小,la

    2022年6月9日
    37
  • ubuntu 小技巧

    ubuntu 小技巧

    2021年4月29日
    255
  • qtcpsocket多线程_qtcpsocket接收数据

    qtcpsocket多线程_qtcpsocket接收数据简述最近在写有关网络传输的项目,使用了Qt封装的QTcpSocket,但是发现很多的跨线程导致死机的问题,也许是我了解的不够深入吧。最开始是自己继承一个线程然后把套接字传递到线程使用遇到程序崩溃;否决后使用了QObject::moveToThread()的方式,虽然程序可以正常发送接收数据但是对于QTcpSocket(moveToThread后属于子线程)与主线程的交互并不是很友好,还是存在一些问题;最后是考虑到了QTcpSocket跨线程崩溃说明它本身内置了一些发送或者接收的线程,这样…

    2022年9月9日
    0
  • springsession使用_常见的使用null场景

    springsession使用_常见的使用null场景目录一、同域名下相同项目(集群环境)实现Session共享1.思路2.架构图3.实现步骤一、同域名下相同项目(集群环境)实现Session共享在同一个域名下,比如:www.p2p.com同一个项目,部署了多台tomcat,这就是典型的集群。我们上一篇文章的入门案例就属于这种应用场景,只不过在实际开发的过程中,我们如果存在了tomcat集群,那么肯定会使用nginx进行负载均衡,那么这种情况下我们该如何处理。1.思路我们将上一个阶段的p2p项目实现集群部署下的Ses

    2025年6月6日
    0
  • matlab库函数大全[通俗易懂]

    matlab库函数大全[通俗易懂]附录MATLAB函数参考附录1常用命令附录1.1管理用命令函数名功能描述函数名功能描述addpath增加一条搜索路径rmpath删除一条搜索路径demo运行Matlab演示程序type列出.M文件

    2022年7月17日
    18
  • 一场影响美国大选的战争利器:邮件安全+协作即时

    一场影响美国大选的战争利器:邮件安全+协作即时

    2022年3月4日
    27

发表回复

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

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