鏈接:https://www.cnblogs.com/paddix/p/8905531.html
前言
雖然現在很多開發,都採用了前後端完全分離的模式,即後端只提供數據接口,前端通過AJAX請求獲取數據,完全不需要用到模板引擎。這種方式的優點在於前後端完全分離,並且隨著近幾年前端工程化工具和MVC框架的完善,使得這種模式的維護成本相對來說也更加低一點。但是這種模式不利於SEO,並且在性能上也會稍微差一點,還有一些場景,使用模板引擎會更方便,比如說郵件模板。這篇文章主要討論Spring boot與模板引擎Thymeleaf、Freemaker以及JSP的集成。
一、集成Thymeleaf
第一步:引入jar包(thymeleaf對應的starter):
<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-thymeleaf/<artifactid>
第二步:配置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: 是否緩存,開發模式下設置為false,避免改了模板還要重啟服務器,線上設置為true,可以提高性能。
encoding&content-type:這個大家應該比較熟悉了,與Servlet中設置輸出對應屬性效果一致。
mode:這個還是參考官網的說明吧,並且這個是2.X與3.0不同,本文自動引入的包是2.15。
第三步 編寫thymeleaf模板文件:
<code>
Thymeleaf 模板引擎
<table>
<thead>
序號
標題
摘要
創建時間
/<thead>
<tbody>
/<tbody>
/<table>
/<code>
大家可以看到,thymeleaf還是比較簡單的,並且最大的特點就是的標籤是作為HTML元素的屬性存在的,也就是說,該頁面是可以直接通過瀏覽器來預覽的,只是沒有數據而已,這個很方便大家進行調試。
第四步 配置Controller:
<code>@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";
}
}/<article>/<code>
注意,這裡用的註解是@Controller,而不是@RestController,因為@RestController會自動將返回結果轉為字符串。
第五步 查看結果
二、Spring boot與Freemarker的集成
1、引入jar包(Freemarker對應的starter)
<code><dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-freemarker/<artifactid>
/<dependency>/<code>
2、配置freemarker:
spring:
freemarker:
template-loader-path: classpath:/templates/
suffix: .ftl
content-type: text/html
charset: UTF-8
settings:
number_format: '0.##'
除了settings外,其他的配置選項和thymeleaf類似。settings會對freemarker的某些行為產生影響,如日期格式化,數字格式化等,感興趣的同學可以參考官網提供的說明:https://freemarker.apache.org/docs/api/freemarker/template/Configuration.html#setSetting-java.lang.String-java.lang.String-
3、編寫freemarker模板文件:
<code>
<title>文章列表/<title>Freemarker 模板引擎
<table>
<thead>
序號
標題
摘要
創建時間
/<thead>
${article.id}
${article.title}
${article.summary}
${article.createTime?string('yyyy-MM-dd hh:mm:ss')}
#list>
/<table>
/<code>
4、編寫Controller:
<code>
@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";
}
}/<article>/<code>
5、訪問頁面:
三、Sring boot與JSP集成:
在正式的項目開發中,現在已經極少用jsp模板了,所以Spring boot對jsp的支持也不是很好,因此配置起來比thymeleaf和Freemaker相對來說就更復雜一點。
第一步 引入jar包:
<code>
<dependency>
<groupid>javax.servlet/<groupid>
<artifactid>jstl/<artifactid>
/<dependency>
<dependency>
<groupid>org.apache.tomcat.embed/<groupid>
<artifactid>tomcat-embed-jasper/<artifactid>
/<dependency>/<code>
第一個jstl的依賴用於支持el表達式,第二個依賴用於支持jsp。注意,如果是在外部的tomcat中運行,需要將scope設置為provide,防止jar包衝突。
第二步 手動創建webapp目錄:
需要手動在main目錄下創建一個webapp的目錄,結構如下:
第三步 jsp路勁配置:
在application.yml中添加如下配置:
spring:
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
瞭解Spring mvc的應該很熟悉上面的配置。
第四步 編寫jsp頁面:
<code>
<title>Title/<title>
<table>
<foreach>
${article.id}
${article.title}
${article.summary}
${article.createTime}
/<foreach>
/<table>
/<code>
第五步 編寫Controller:
<code>@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";
}/<article>/<code>
第六步 訪問結果頁面:
四、總結
總體來講,Spring boot對thymeleaf和Freemaker支持比較友好,配置相對也簡單一點,在實際的開發中,大多也以這兩種模板引擎為主,很少有用jsp的,jsp現在可能更多是在實驗或者學習階段使用。jsp配置比較麻煩一點的事情是不像前兩者,網上的說法基本一致,但是對Jsp的配置有很多種說法,比如說是不是需要將jar包改成war包?jsp的依賴是否需要設置為provide等等,這個主要依賴於你是否最後要將程序部署到外部的tomcat還是直接運行jar?因為本文都是直接在idea下直接運行Application類,所以這些操作就不需要了。
閱讀更多 牛旦教育IT課堂 的文章