第三章 SpringBoot整合視圖層技術(shù)

1、整合thymeleaf(官方推薦)

1.1 thymeleaf是什么

java新一代的模板引擎,支持html原型,可以讓前端工程師
直接瀏覽器查看樣式,也可以支持后端工程師結(jié)合真實(shí)數(shù)據(jù)查看效果。

1.2 整合步驟
  • 添加依賴
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>1.5.12.RELEASE</version>
        </dependency>
    </dependencies>
  • 配置thymeleaf
    ctrl+shift+r全局搜索ThymeleafAutoConfiguration類,這是springboot為thymeleaf提供的配置類。而相關(guān)配置屬性在ThymeleafProperties這個(gè)類中。


    image.png
  • 修改thymeleaf的默認(rèn)配置(在application.properties)
    部分常見配置


    image.png
1.3 實(shí)戰(zhàn)
  • 創(chuàng)建maven工程,不需要選擇項(xiàng)目框架
    pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ljs</groupId>
    <artifactId>thymeleaf</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>thymeleaf</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <!--統(tǒng)一項(xiàng)目字符集編碼和java版本-->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <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>
    </dependencies>
</project>
  • springboot啟動(dòng)類,使用組合注解
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
  • 實(shí)體類和Controller類
public class Book {

    private Integer id;
    private String name;
    private String author;

//省略get和set
@Controller
public class BookController {

    @GetMapping("/books")
    public ModelAndView Books(){

        List<Book> books = new ArrayList<>();
        Book book1 = new Book();
        book1.setId(1);
        book1.setName("springboot");
        book1.setAuthor("王松");

        Book book2 = new Book();
        book2.setId(2);
        book2.setName("vue");
        book2.setAuthor("王松");

        books.add(book1);
        books.add(book2);

        ModelAndView mv = new ModelAndView();
        mv.addObject("books", books);
        //視圖名為books
        mv.setViewName("books");
        return mv;
    }
}
  • 創(chuàng)建模板
    在resource下創(chuàng)建templates文件夾,創(chuàng)建books.html
    注意第二行導(dǎo)入thymeleaf的名稱空間
<!DOCTYPE html>
<html lang="en" xmln:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>圖書列表</title>
</head>
<body>
<table border="1">
    <tr>
        <td>id</td>
        <td>書名</td>
        <td>作者</td>
    </tr>
    <tr th:each="book:${books}">
        <td th:text="${book.id}"/>
        <td th:text="${book.name}"/>
        <td th:text="${book.author}"/>
    </tr>
</table>

</body>
</html>
  • 啟動(dòng)運(yùn)行


    image.png
1.5 更多關(guān)于thymeleaf的基礎(chǔ)用法,

thymeleaf官網(wǎng)

2、整合FreeMarker

2.1 freemarker

與Thymeleaf不同 FreeMarker 需要經(jīng)過解析才能夠在瀏覽器中展示出來。 freeMarker不僅可以用來配置HTML頁面模板,也可以作為電子郵件模板、配置文件模板以及源碼模板等。

2.2 整合步驟
  • 添加依賴
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
  • 配置freemarker
    SpringBoot對FreeMarker也提供了自動(dòng)化配置類 FreeMarkerAutoConfiguration ,相關(guān)的配屬性在FreeMarkerProperties
    ctrl+shift+r全局搜索FreeMarkerProperties類


    image.png
  • 修改freemarker默認(rèn)配置(application.properties)


    image.png
3.3 實(shí)戰(zhàn)
  • 這次直接創(chuàng)建springboot工程,而不是maven工程


    image.png
  • pom文件直接加入上面兩個(gè)依賴就行

  • 實(shí)體類和controller和上面一樣

  • 模板文件
    其中先判斷model中的books不為空并且有數(shù)據(jù),然后再遍歷

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>圖書列表</title>
</head>
<body>
<hr>
<table border="1">
    <tr>
        <td>圖書編號(hào)</td>
        <td>圖書名稱</td>
        <td>圖書作者</td>
    </tr>
<#if books ??&&(books?size>0)>
    <#list books as book>
        <tr>
            <td>${book.id}</td>
            <td>${book.name}</td>
            <td>${book.author}</td>
        </tr>
    </#list>
</#if>
</table>
</body>
</html>

3 小結(jié)

本章介紹了springboot整合視圖層技術(shù),如果項(xiàng)目未完全分離可以使用上面兩種代替jsp,如果完全分離后端只需提供接口而不需要整合這些。

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

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

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