03-SpirngBoot 整合視圖層技術(shù)

雖然現(xiàn)在前后端大多是分離的,而且是使用json數(shù)據(jù)傳輸,但是多少我們都要知道一些整合技術(shù)的使用。

早些時(shí)候,我們使用JSP進(jìn)行頁(yè)面數(shù)據(jù)開發(fā),如今,JSP已經(jīng)逐漸退下舞臺(tái)。

對(duì)于后端開發(fā)工程師,最好的頁(yè)面交互式使用JSON數(shù)據(jù)。

本章節(jié)作為開發(fā)參考。

[TOC]


目錄.jpg

JSP的接班

當(dāng)今企業(yè)級(jí)的開發(fā)中,前后端是分離的,對(duì)于視圖層技術(shù)有專業(yè)人士去開發(fā),但是SB中對(duì)于視圖層也有支持,當(dāng)即推薦的就是Thymeleaf,以及Freemarker,他們是JSP的的后輩。

Thymeleaf支持HTML原型,方便前端工程師查看樣式,方便后端工程師查看效果。

Thymeleaf的使用

是什么

SB推薦的前端模板引擎,支持HTML原型??梢越Y(jié)合HTML,將數(shù)據(jù)以對(duì)象的形式進(jìn)行顯示。

添加依賴

在pom中添加spring-boot-starter-thymeleaf依賴。

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

配置Thymeleaf

按理說:自動(dòng)化配置類ThymeleafAutoConfiguration.class會(huì)生成,但是筆者并未看到。

ThymeleafAutoConfiguration.class這個(gè)配置類主要是用來進(jìn)行配置的,如果沒有生成,無妨,自己生成即可,但是不推薦這么做,因?yàn)槲覀兏呐渲猛ǔ6加胮roperties進(jìn)行配置。

application.properties中有相關(guān)配置(常用):

# >>>>>>>>>>>>>>>>>>>>>>>>>Thymeleaf config
# 是否開啟緩存,默認(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; charset=utf-8
# 模板文件后綴
spring.thymeleaf.suffix=.html

配置控制器

創(chuàng)建一個(gè)POJO對(duì)象:Book:

public class Book {

    private Integer id;
    private String name,author;
    
    ……geter/seter
    
}

創(chuàng)建一個(gè)控制類方法:

    @GetMapping("/book")
    public ModelAndView books() {
        
        System.out.println(">>>>>>>>>>>>>>>>>>>>s");
        
        Book b = new Book();
        b.setId(1);
        b.setAuthor("Leon");
        b.setName("西行記");
        
        Book b2 = new Book();
        b2.setId(1);
        b2.setAuthor("Leon");
        b2.setName("西行記");
        
        ArrayList<Book> books = new ArrayList<Book>();
        books.add(b);
        books.add(b2);
        
        ModelAndView mModelAndView = new ModelAndView();
        mModelAndView.addObject("books",books); // 定義數(shù)據(jù)對(duì)象
        mModelAndView.setViewName("book"); // 跳轉(zhuǎn)到templates中指定頁(yè)面book.html
        
        return mModelAndView;
    }

請(qǐng)注意setViewName方法他的作用是指向頁(yè)面到resources/templates/book.html

視圖顯示

在resource下新建templates文件,在里面新建文件book.html

<!DOCTYPE html>
<html lang='en' xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</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>

運(yùn)行即可顯示。

FreeMarker

配置方式方法跟Thymeleaf類似,在視圖顯示的時(shí)候會(huì)有稍許不同。

請(qǐng)各位看官自行百度即可。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容