SpringBoot整合Thymeleaf

Thymeleaf是新一代的Java模版引擎,支持HTML原型,既可以讓前端工程師在瀏覽器中直接打開查看樣式,也可以讓后端工程師結(jié)合數(shù)據(jù)查看真實(shí)的效果。同時(shí)SpringBoot提供了Thymeleaf的自動(dòng)化配置解決方案,因此在SpringBoot中使用Thymeleaf將會(huì)非常的方便。
步驟01:創(chuàng)建工程添加依賴

<!--引入thymeleaf的相關(guān)依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
<!--引入Web的相關(guān)依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

步驟02:配置Thymeleaf

#thymeleaf的配置
#是否開啟緩存,開發(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

步驟03:配置Tomact容器

#配置WEB容器的端口號(hào)
server.port=8080
#配置了當(dāng)前項(xiàng)目出錯(cuò)的跳轉(zhuǎn)頁面
server.error.path=/error
#配置了Session的失效時(shí)間30分鐘如果不寫單位默認(rèn)就是秒
server.servlet.session.timeout=30m
#表示項(xiàng)目名稱如果不配置默認(rèn)為/ 如果配置了就要在訪問路徑上加上配置的路徑
server.servlet.context-path=/springboot
#配置了Tomact的請(qǐng)求編碼
server.tomcat.uri-encoding=utf-8
#表示Tomact的最大線程數(shù)
server.tomcat.max-threads=500
#存放Tomact運(yùn)行日志的文件
#server.tomcat.basedir=/home/sang/tmp

步驟04:配置實(shí)體類

package com.gsww.lf.springboot01.com.gsww.lf.springboot01.entity;

import org.springframework.stereotype.Repository;

/**
 * @Auther:yq
 * @Date:2019/6/25
 * @Description:com.gsww.lf.springboot01.com.gsww.lf.springboot01.entity
 * @Version:1.0
 */
@Repository
public class Book {
    private Integer id;
    private String name;
    private String author;

    public Book(Integer id, String name, String author) {
        this.id = id;
        this.name = name;
        this.author = author;
    }

    public Book() {
    }

    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 + '\'' +
                '}';
    }
}

步驟05:配置控制器

package com.gsww.lf.springboot01.com.gsww.ls.springboot01.controller;

import com.gsww.lf.springboot01.com.gsww.lf.springboot01.entity.Book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;

/**
 * @Auther:yq
 * @Date:2019/6/25
 * @Description:com.gsww.lf.springboot01.com.gsww.ls.springboot01.controller
 * @Version:1.0
 */
@Controller
public class BookController {

    @GetMapping("/books")
    public ModelAndView books(){
        ArrayList<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 md = new ModelAndView();
        md.addObject("books",books);
        md.setViewName("books");
        return md;

    }

}


步驟06:創(chuàng)建視圖

<!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>圖書編號(hào)</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>

步驟07:運(yùn)行
在瀏覽器地址欄中輸入http://localhost:8080/springboot/books即可看到運(yùn)行效果如圖:

image.png

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容