博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot整合Thymeleaf模板
阅读量:3946 次
发布时间:2019-05-24

本文共 3750 字,大约阅读时间需要 12 分钟。

整合Thymeleaf步骤

使用工具:

JDK:1.8
开发工具:IDEA
我使用的idea版本是2019的
项目目录:
在这里插入图片描述

1.创建工程,添加依赖

新建一个Spring Boot工程,然后添加spring-boot-starter-web 和 spring-boot-starter-thymeleaf依赖,代码如下:

org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web

2.配置Thymeleaf

Spring Boot为 Thymeleaf提供了自动化配置类 ThymeleafAutoConfigutaion,相关的配置属性在ThymeleafProperties类中,ThymeleafProperties部分源码如下:

@ConfigurationProperties(    prefix = "spring.thymeleaf")public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING; public static final String DEFAULT_PREFIX = "classpath:/templates/"; public static final String DEFAULT_SUFFIX = ".html"; private boolean checkTemplate = true; private boolean checkTemplateLocation = true; private String prefix = "classpath:/templates/"; private String suffix = ".html"; private String mode = "HTML";

由此配置可以看出,模板的默认位置在 classpath:/templates/,默认的模板后缀为.html。如果使用 Intellij IDEA工具创建Spring Boot项目,templates文件夹默认就会创建。

当然,如果开发者想对默认的Thymeleaf配置参数进行自定义修改,可以直接在application.properties中进行配置,部分常见配置如下:

#是否开启缓存,开发时可设置为false,默认为truespring.thymeleaf.cache=true#检查模板是否存在,默认为truespring.thymeleaf.check-template=true#检查模板位置是否存在spring.thymeleaf.check-template-location=true#模板文件编码spring.thymeleaf.encoding=UTF-8#模板文件位置spring.thymeleaf.prefix=classpath:/templates/#Content-Type配置spring.thymeleaf.servlet.content-type=text/html#模板后缀名spring.thymeleaf.suffix=.html

这里也是给大家一个非常好的建议,多看看源码有好处,当然要循环俱进,这对新手提升有很大的帮助的!加油!!!

3.配置控制器

  • 3-1创建Book实体类,然后在Controller中返回ModelAndView,代码如下:
package org.sang.chapter01.model;import java.io.Serializable;public class Book implements Serializable {    private Integer id;	//图示编号    private String name;	//图书名称    private String author;	//图书作者    public Book() {    }    public Book(Integer id, String name, String author) {        this.id = id;        this.name = name;        this.author = author;    }    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getAuthor() {        return author;    }    public void setAuthor(String author) {        this.author = author;    }    @Override    public String toString() {        return "Book{" +                "id=" + id +                ", name='" + name + '\'' +                ", author='" + author + '\'' +                '}';    }}
  • 3-2控制层代码如下:
package org.sang.chapter01.controller;import org.sang.chapter01.model.Book;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.servlet.ModelAndView;import java.util.ArrayList;import java.util.List;@Controllerpublic class BookController {    @GetMapping("/books")    public ModelAndView books(){        List
books = new ArrayList<>(); Book b1 = new Book(1,"三国演义","罗贯中"); Book b2 = new Book(2,"红楼梦","曹雪芹"); books.add(b1); books.add(b2); ModelAndView mv = new ModelAndView(); mv.addObject("books",books); mv.setViewName("books"); return mv; }}

代码解释:创建Book实体类,承载返回数据。

在BookController方法中1-5行为构建返回数据,第6-9行创建ModelAndView,设置视图名为books,返回数据为所创建的List集合
4.创建视图
在resource目录下的templates目录中创建books.html,具体代码如下:

    
图书列表
图书编号 图书名称 图书作者

代码解释

首先在第2行导入Thymeleaf的名称空间。
然后通过遍历将books中的数据展示出来,Thymeleaf中通过 th:each 进行集合遍历,通过 th:text 展示数据
5.运行
在浏览器地址栏输入 “http://localhost:8080/books”,即可看到运行结果
在这里插入图片描述
大家如果对Thymeleaf感兴趣的话,可以查看Thymeleaf模板官网:

转载地址:http://uiqwi.baihongyu.com/

你可能感兴趣的文章
JAD中常见字段的介绍。
查看>>
对于SIM menu update by OTA的解释
查看>>
如何去掉OTA provisioning的PIN码验证
查看>>
如何实现首次开机自动power on 蓝牙?
查看>>
Makefile文件中蓝牙相关的宏介绍
查看>>
如何屏蔽掉乱码的push消息
查看>>
登录一些安全网站,比如twitter/facebook,提示安全链接失败,或提示下载文件。
查看>>
如何去除launcher 上默认的 google search bar.
查看>>
Android如何绘制View
查看>>
23种设计模式的索引
查看>>
Can't make emergency call without SIM card
查看>>
不插SIM卡,不能拨打紧急电话
查看>>
COSMOS上实现IP DIAL功能
查看>>
什么话不可以和上司讲
查看>>
请学会淘汰你的上司
查看>>
以人为本
查看>>
全球分布式创新:企业致胜的关键
查看>>
上司最恨员工哪十大"罪行"
查看>>
和上司沟通必备8个黄金句
查看>>
竹笋和榕树的管理学
查看>>