您好,登錄后才能下訂單哦!
雖然現(xiàn)在很多開(kāi)發(fā),都采用了前后端完全分離的模式,即后端只提供數(shù)據(jù)接口,前端通過(guò)AJAX請(qǐng)求獲取數(shù)據(jù),完全不需要用的模板引擎。這種方式的優(yōu)點(diǎn)在于前后端完全分離,并且隨著近幾年前端工程化工具和MVC框架的完善,使得這種模式的維護(hù)成本相對(duì)來(lái)說(shuō)也更加低一點(diǎn)。但是這種模式不利于SEO,并且在性能上也會(huì)稍微差一點(diǎn),還有一些場(chǎng)景,使用模板引擎會(huì)更方便,比如說(shuō)郵件模板。這篇文章主要討論Spring boot與模板引擎Thymeleaf、Freemaker以及JSP的集成。
一、集成Thymeleaf
第一步:引入jar包(thymeleaf對(duì)應(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: 是否緩存,開(kāi)發(fā)模式下設(shè)置為false,避免改了模板還要重啟服務(wù)器,線上設(shè)置為true,可以提高性能。
encoding&content-type:這個(gè)大家應(yīng)該比較熟悉了,與Servlet中設(shè)置輸出對(duì)應(yīng)屬性效果一致。
mode:這個(gè)還是參考官網(wǎng)的說(shuō)明吧,并且這個(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> <h7>Thymeleaf 模板引擎</h7> <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還是比較簡(jiǎn)單的,并且最大的特點(diǎn)就是的標(biāo)簽是作為HTML元素的屬性存在的,也就是說(shuō),該頁(yè)面是可以直接通過(guò)瀏覽器來(lái)預(yù)覽的,只是沒(méi)有數(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對(duì)應(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ì)對(duì)freemarker的某些行為產(chǎn)生影響,如日期格式化,數(shù)字格式化等,感興趣的同學(xué)可以參考官網(wǎng)提供的說(shuō)明:https://freemarker.apache.org/docs/api/freemarker/template/Configuration.html#setSetting-java.lang.String-java.lang.String-
3、編寫freemarker模板文件:
<html> <title>文章列表</title> <body> <h7>Freemarker 模板引擎</h7> <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、訪問(wèn)頁(yè)面:
三、Sring boot與JSP集成:
在正式的項(xiàng)目開(kāi)發(fā)中,現(xiàn)在已經(jīng)極少用jsp模板了,所以Spring boot對(duì)jsp的支持也不是很好,因此配置起來(lái)比thymeleaf和Freemaker相對(duì)來(lái)說(shuō)就更復(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頁(yè)面:
<%@ 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"; }
第六步 訪問(wèn)結(jié)果頁(yè)面:
四、總結(jié)
總體來(lái)講,Spring boot對(duì)thymeleaf和Freemaker支持比較友好,配置相對(duì)也簡(jiǎn)單一點(diǎn),在實(shí)際的開(kāi)發(fā)中,大多也以這兩種模板引擎為主,很少有用jsp的,jsp現(xiàn)在可能更多是在實(shí)驗(yàn)或者學(xué)習(xí)階段使用。jsp配置比較麻煩一點(diǎn)的事情是不像前兩者,網(wǎng)上的說(shuō)法基本一致,但是對(duì)Jsp的配置有很多種說(shuō)法,比如說(shuō)是不是需要將jar包改成war包?jsp的依賴是否需要設(shè)置為provide等等,這個(gè)主要依賴于你是否最后要將程序部署到外部的tomcat還是直接運(yùn)行jar?因?yàn)楸疚亩际侵苯釉趇dea下直接運(yùn)行Application類,所以這些操作就不需要了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。