SpringBoot2.x系列:整合SpringMVC之模板引擎

其實現在的很多web項目開發,都採用了前後端完全分離的模式,也就是說後端只提供數據接口,前端通過AJAX請求獲取數據,所以完全不需要用的模板引擎。

但是前後端分離的這種模式也有缺點,比如不利於SEO,並且在性能上也會稍微差一點。另外還有一些場景,使用模板引擎會更方便,比如說使用郵件模板。

所以本章節給大家簡單介紹在Spring boot中使用Thymeleaf、Freemaker等模板引擎以及對JSP的集成。

一. SpringMVC的模板技術

Spring MVC支持各種各樣的模板技術,包括Velocity, FreeMarker和JSP等,很多其他的模板引擎也提供了它們自己與Spring MVC集成的API。

1. Spring Boot支持的模板引擎

Spring Boot支持以下的模板引擎,爲他們提供了自動配置。

  • FreeMarker
  • Groovy
  • Thymeleaf
  • Velocity(1.4已不再支持)
  • Mustache

默認情況下,對以上的任意模板引擎,Spring Boot都會默認從src/main/resources/templates目錄下自動加載模板引擎文件。

2. 使用注意事項

1.由於在內嵌的servlet容器中使用JSP會存在一些限制,所以建議儘量不要在Spring Boot中使用JSP。

2.IntelliJ IDEA會根據運行應用的方式不同,會對classpath進行不同的排序。在IDEA裏通過main方法運行應用,跟從Maven,或Gradle,或打包好的jar中運行相比,會導致不同的順序,這可能導致Spring Boot不能從classpath下成功地找到模板。

如果遇到這個問題,你可以在IDEA裏重新對classpath進行排序,將模塊的類和資源放到第一位。比如可以配置模塊的前綴爲classpath*:/templates/,這樣會查找classpath下的所有模板目錄。

模板引擎有好幾個,本章節就就講解最常用的Thymeleaf,FreeMarker及與jsp的結合實現。

二. SpringBoot中整合Thymeleaf

我在前面的章節中,已經講過SpringBoot與Thymeleaf的整合,所以本節略過。

請參考我前面的章節:
15_SpringBoot2.x系列教程15--Web開發01之Thymeleaf使用

知乎地址:

https://zhuanlan.zhihu.com/p/113864980

CSDN地址:

https://blog.csdn.net/syc000666/article/details/105087519

三. SpringBoot中整合JSP

我在前面的章節中,已經講過SpringBoot與JSP的整合,所以本節略過。

請參考我前面的章節:
17_SpringBoot2.x系列教程17--Web開發03之支持jsp

知乎地址:

https://zhuanlan.zhihu.com/p/114074328

CSDN地址:

https://blog.csdn.net/syc000666/article/details/105087545

四. SpringBoot中整合FreeMarker

1. FreeMarker簡介

FreeMarker是一個不錯的模板引擎,在衆多的網頁靜態化模板中口碑很好,今天就用Spring Boot來整合這個模板。

2. 創建web項目

我們創建一個Spring Boot項目,該項目結構如下。

3. 添加FreeMarker依賴

在pom.xml文件中添加FreeMarker依賴包。

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

4. 添加FreeMarker配置信息

在application.properties文件中,添加FreeMarker的一些配置信息。
spring.freemarker.template-loader-path指的是freemarker文件的路徑信息;
spring.freemarker.cache這個表示的是緩存是否打開;
spring.freemarker.suffix=.ftl指明瞭freemarker文件的後綴名爲.ftl;
其他幾個都是常規配置,基本不需要修改的。

#.ftl文件存放位置
spring.freemarker.template-loader-path=classpath:/templates/
#spring.freemarker.prefix=
spring.freemarker.suffix=.ftl
spring.freemarker.cache=false
spring.freemarker.charset=utf-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.allow-request-override=true
spring.freemarker.allow-session-override=true
spring.freemarker.request-context-attribute=request

5. 創建資源文件

在resources目錄下創建一個資源文件conf.properties,作爲測試的數據源。

users.name=yiyige
users.desc=learn freemarker

6. 創建.ftl模板文件

在templates目錄下,再創建一個ftl目錄,裏面創建一個模板文件index.ftl。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>User</title>
</head>
<body>
<h1>${user.name}</h1>
<h1>${user.desc}</h1>
</body>
</html>

7. 創建讀取資源文件的實體類

創建一個實體類,用於從conf.properties文件中讀取配置信息。該類中使用了@Component,@ConfigurationProperties,@PropertySource三個註解,實體類屬性對應資源文件,並添加Setter和Getter方法。

package com.yyg.boot;

import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * @Description Description
 * @Author 一一哥Sun
 * @Date Created in 2020/3/25
 */
@Data
@ToString
@Component
@ConfigurationProperties(prefix = "users")
@PropertySource(value = "classpath:/conf.properties")
public class User {

    private String name;

    private String desc;

}

8. 創建Controller測試接口

創建controller類,添加調試接口方法,把資源數據通過Model傳送到index.ftl上,這裏的返回字符串上不用加後綴,因爲默認.ftl。

package com.yyg.boot.web;

import com.yyg.boot.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @Description Description
 * @Author 一一哥Sun
 * @Date Created in 2020/3/25
 */
@Controller
public class HelloController {

    @Autowired
    private User user;

    @GetMapping("/show")
    public String show(Model model) {
        model.addAttribute("user", user);
        //ftl是.ftl模板文件所在文件夾,index是模板名稱.
        return "ftl/index";
    }

}

9. 創建啓動類

package com.yyg.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @Description Description
 * @Author 一一哥Sun
 * @Date Created in 2020/3/25
 */
@SpringBootApplication
public class TemplateApplication {

    public static void main(String[] args) {
        SpringApplication.run(TemplateApplication.class, args);
    }

}

10. 啓動程序測試

在瀏覽器中輸入地址
http://localhost:8080/show
可以看到如下界面效果,說明我們已經成功的在Spring Boot中集成了FreeMarker模板,渲染了出了模板頁面。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章