本文共 3750 字,大约阅读时间需要 12 分钟。
使用工具:
JDK:1.8 开发工具:IDEA 项目目录: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.配置控制器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 + '\'' + '}'; }}
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(){ Listbooks = 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/