亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot整合Thymeleaf的方法

 更新時(shí)間:2020年02月15日 15:42:31   作者:crazy戴夫  
這篇文章主要介紹了SpringBoot整合Thymeleaf的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

簡(jiǎn)介:

  在目前的企業(yè)級(jí)應(yīng)用開(kāi)發(fā)中 前后端分離是趨勢(shì),但是視圖層技術(shù)還占有一席之地, Spring Boot 對(duì)視圖層技術(shù)提供了很好的支持,官方推薦使用的模板引擎是 Thymeleaf 不過(guò)像 FreeMarker 也支持, JSP 技術(shù)在這里并不推薦使用。

  Thymeleaf 是新一代 Java 模板引擎,類(lèi)似于 Velocity、FreeMarker 等傳統(tǒng) Java 模板引擎。與傳統(tǒng) Java 模板引擎不同的是 Thymeleaf 支持 HTML 原型,既可 以讓前端工程師在瀏覽器中直接打 開(kāi)查看樣式,也可以讓后端工程師結(jié)合真實(shí)數(shù)據(jù)查看顯示效果。 同時(shí), Spring Boot 提供了 Thymeleaf 自動(dòng) 配置解決方案,因此Spring Boot 中使用 Thymeleaf 常方便。

1.引入依賴(lài):

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

2.application.properties

#是否開(kāi)啟緩存,開(kāi)發(fā)時(shí)可設(shè)置為false,默認(rèn)為true
spring.thymeleaf.cache=true
#是否檢查模板是否存在,默認(rèn)為true
spring.thymeleaf.check-template=true
#是否檢查模板位置是否存在,默認(rèn)為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.創(chuàng)建實(shí)體類(lèi)和controller類(lèi)

public class Book {
  private Integer id;
  private String name;
  private String author;
  //省略getter/setter
}
@Controller
public class BookController {
  @GetMapping("/books")
  public ModelAndView books() {
    List<Book> books = new ArrayList<>();
    Book b1 = new Book();
    b1.setId(1);
    b1.setAuthor("羅貫中");
    b1.setName("三國(guó)演義");
    Book b2 = new Book();
    b2.setId(2);
    b2.setAuthor("曹雪芹");
    b2.setName("紅樓夢(mèng)");
    books.add(b1);
    books.add(b2);
    ModelAndView mv = new ModelAndView();
    mv.addObject("books", books);
    mv.setViewName("books");
    return mv;
  }
}

4.html文件:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>圖書(shū)列表</title>
</head>
<body>
<table border="1">
  <tr>
    <td>圖書(shū)編號(hào)</td>
    <td>圖書(shū)名稱(chēng)</td>
    <td>圖書(shū)作者</td>
  </tr>
  <tr th:each="book:${books}">
    <td th:text="${book.id}"></td>
    <td th:text="${book.name}"></td>
    <td th:text="${book.author}"></td>
  </tr>
</table>
</body>
</html>

5.結(jié)果:

總結(jié)

以上所述是小編給大家介紹的SpringBoot整合Thymeleaf的方法,希望對(duì)大家有所幫助!

相關(guān)文章

最新評(píng)論