欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot整合Thymeleaf的方法

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

簡介:

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

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

1.引入依賴:

 <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

#是否開啟緩存,開發(fā)時可設(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)建實體類和controller類

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("三國演義");
    Book b2 = new Book();
    b2.setId(2);
    b2.setAuthor("曹雪芹");
    b2.setName("紅樓夢");
    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>圖書列表</title>
</head>
<body>
<table border="1">
  <tr>
    <td>圖書編號</td>
    <td>圖書名稱</td>
    <td>圖書作者</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的方法,希望對大家有所幫助!

相關(guān)文章

最新評論