Spring Boot實(shí)戰(zhàn)之模板引擎
雖然現(xiàn)在很多開發(fā),都采用了前后端完全分離的模式,即后端只提供數(shù)據(jù)接口,前端通過AJAX請求獲取數(shù)據(jù),完全不需要用的模板引擎。這種方式的優(yōu)點(diǎn)在于前后端完全分離,并且隨著近幾年前端工程化工具和MVC框架的完善,使得這種模式的維護(hù)成本相對來說也更加低一點(diǎn)。但是這種模式不利于SEO,并且在性能上也會(huì)稍微差一點(diǎn),還有一些場景,使用模板引擎會(huì)更方便,比如說郵件模板。這篇文章主要討論Spring boot與模板引擎Thymeleaf、Freemaker以及JSP的集成。
一、集成Thymeleaf
第一步:引入jar包(thymeleaf對應(yīng)的starter):
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
第二步:配置thymeleaf:
spring: thymeleaf: prefix: classpath:/templates/ check-template-location: true cache: false suffix: .html encoding: UTF-8 content-type: text/html mode: HTML5
prefix:指定模板所在的目錄
check-tempate-location: 檢查模板路徑是否存在
cache: 是否緩存,開發(fā)模式下設(shè)置為false,避免改了模板還要重啟服務(wù)器,線上設(shè)置為true,可以提高性能。
encoding&content-type:這個(gè)大家應(yīng)該比較熟悉了,與Servlet中設(shè)置輸出對應(yīng)屬性效果一致。
mode:這個(gè)還是參考官網(wǎng)的說明吧,并且這個(gè)是2.X與3.0不同,本文自動(dòng)引入的包是2.15。
第三步 編寫thymeleaf模板文件:
<!DOCTYPE HTML> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta content="text/html;charset=UTF-8"/> </head> <body> <h6>Thymeleaf 模板引擎</h6> <table border="1" bgcolor="#f0ffff"> <thead> <tr> <th>序號(hào)</th> <th>標(biāo)題</th> <th>摘要</th> <th>創(chuàng)建時(shí)間</th> </tr> </thead> <tbody th:each="article : ${list}"> <tr> <td th:text="${article.id}"></td> <td th:text="${article.title}"></td> <td th:text="${article.summary}"></td> <td th:text="${article.createTime}"></td> </tr> </tbody> </table> </body> </html>
大家可以看到,thymeleaf還是比較簡單的,并且最大的特點(diǎn)就是的標(biāo)簽是作為HTML元素的屬性存在的,也就是說,該頁面是可以直接通過瀏覽器來預(yù)覽的,只是沒有數(shù)據(jù)而已,這個(gè)很方便大家進(jìn)行調(diào)試。
第四步 配置Controller:
@Controller @RequestMapping("/article") public class ArticleController { @Autowired private ArticleService articleService; @RequestMapping("/articleList.html") public String getArticleList(Model model, String title, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageNum) { int offset = (pageNum - 1) * pageSize; List<Article> list = articleService.getArticles(title, 1L, offset, pageSize); model.addAttribute("list", list); return "article/articleList"; } }
注意,這里用的注解是@Controller,而不是@RestController,因?yàn)锧RestController會(huì)自動(dòng)將返回結(jié)果轉(zhuǎn)為字符串。
第五步 查看結(jié)果
二、Spring boot與Freemarker的集成
1、引入jar包(Freemarker對應(yīng)的starter)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
2、配置freemarker:
spring: freemarker: template-loader-path: classpath:/templates/ suffix: .ftl content-type: text/html charset: UTF-8 settings: number_format: '0.##'
除了settings外,其他的配置選項(xiàng)和thymeleaf類似。settings會(huì)對freemarker的某些行為產(chǎn)生影響,如日期格式化,數(shù)字格式化等,感興趣的同學(xué)可以參考官網(wǎng)提供的說明:https://freemarker.apache.org/docs/api/freemarker/template/Configuration.html#setSetting-java.lang.String-java.lang.String-
3、編寫freemarker模板文件:
<html> <title>文章列表</title> <body> <h6>Freemarker 模板引擎</h6> <table border="1"> <thead> <tr> <th>序號(hào)</th> <th>標(biāo)題</th> <th>摘要</th> <th>創(chuàng)建時(shí)間</th> </tr> </thead> <#list list as article> <tr> <td>${article.id}</td> <td>${article.title}</td> <td>${article.summary}</td> <td>${article.createTime?string('yyyy-MM-dd hh:mm:ss')}</td> </tr> </#list> </table> </body> </html>
4、編寫Controller:
@Controller @RequestMapping("/article") public class ArticleController { @Autowired private ArticleService articleService; @RequestMapping("/list.html") public String getArticles(Model model, String title, @RequestParam(defaultValue = "10") Integer pageSize, Integer pageNum) { if (pageSize == null) { pageSize = 10; } if (pageNum == null) { pageNum = 1; } int offset = (pageNum - 1) * pageSize; List<Article> list = articleService.getArticles(title, 1L, offset, pageSize); model.addAttribute("list", list); return "article/list"; } }
5、訪問頁面:
三、Sring boot與JSP集成:
在正式的項(xiàng)目開發(fā)中,現(xiàn)在已經(jīng)極少用jsp模板了,所以Spring boot對jsp的支持也不是很好,因此配置起來比thymeleaf和Freemaker相對來說就更復(fù)雜一點(diǎn)。
第一步 引入jar包:
<dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency>
第一個(gè)jstl的依賴用于支持el表達(dá)式,第二個(gè)依賴用于支持jsp。注意,如果是在外部的tomcat中運(yùn)行,需要將scope設(shè)置為provide,防止jar包沖突。
第二步 手動(dòng)創(chuàng)建webapp目錄:
需要手動(dòng)在main目錄下創(chuàng)建一個(gè)webapp的目錄,結(jié)構(gòu)如下:
第三步 jsp路勁配置:
在application.yml中添加如下配置:
spring: mvc: view: prefix: /WEB-INF/jsp/ suffix: .jsp
了解Spring mvc的應(yīng)該很熟悉上面的配置。
第四步 編寫jsp頁面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <title>Title</title> </head> <body> <table border="1"> <c:forEach var="article" items="${list}"> <tr> <td>${article.id}</td> <td>${article.title}</td> <td>${article.summary}</td> <td>${article.createTime}</td> </tr> </c:forEach> </table> </body> </html>
第五步 編寫Controller:
@RequestMapping("/listJsp") public String getArticleListJsp(Model model, String title, @RequestParam(defaultValue = "10") Integer pageSize, Integer pageNum) { if (pageSize == null) { pageSize = 10; } if (pageNum == null) { pageNum = 1; } int offset = (pageNum - 1) * pageSize; List<Article> list = articleService.getArticles(title, 1L, offset, pageSize); model.addAttribute("list", list); return "articles"; }
第六步 訪問結(jié)果頁面:
四、總結(jié)
總體來講,Spring boot對thymeleaf和Freemaker支持比較友好,配置相對也簡單一點(diǎn),在實(shí)際的開發(fā)中,大多也以這兩種模板引擎為主,很少有用jsp的,jsp現(xiàn)在可能更多是在實(shí)驗(yàn)或者學(xué)習(xí)階段使用。jsp配置比較麻煩一點(diǎn)的事情是不像前兩者,網(wǎng)上的說法基本一致,但是對Jsp的配置有很多種說法,比如說是不是需要將jar包改成war包?jsp的依賴是否需要設(shè)置為provide等等,這個(gè)主要依賴于你是否最后要將程序部署到外部的tomcat還是直接運(yùn)行jar?因?yàn)楸疚亩际侵苯釉趇dea下直接運(yùn)行Application類,所以這些操作就不需要了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
處理@PathVariable注解允許參數(shù)為空、允許不傳參數(shù)的問題
這篇文章主要介紹了處理@PathVariable注解允許參數(shù)為空、允許不傳參數(shù)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02SpringBoot數(shù)據(jù)校驗(yàn)功能的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot數(shù)據(jù)校驗(yàn)功能的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02jackson使用@JsonSerialize格式化BigDecimal解決.00不顯示問題
這篇文章主要介紹了jackson使用@JsonSerialize格式化BigDecimal解決.00不顯示問題,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-02-02sharding-jdbc5.0.0實(shí)現(xiàn)分表實(shí)踐
本文主要介紹了sharding-jdbc5.0.0分表實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02JFileChooser實(shí)現(xiàn)對選定文件夾內(nèi)圖片自動(dòng)播放和暫停播放實(shí)例代碼
這篇文章主要介紹了JFileChooser實(shí)現(xiàn)對選定文件夾內(nèi)圖片自動(dòng)播放和暫停播放實(shí)例代碼,需要的朋友可以參考下2017-04-04