国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Java > 正文

Spring Boot實戰(zhàn)之模板引擎

2024-07-14 08:40:43
字體:
來源:轉載
供稿:網友

雖然現在很多開發(fā),都采用了前后端完全分離的模式,即后端只提供數據接口,前端通過AJAX請求獲取數據,完全不需要用的模板引擎。這種方式的優(yōu)點在于前后端完全分離,并且隨著近幾年前端工程化工具和MVC框架的完善,使得這種模式的維護成本相對來說也更加低一點。但是這種模式不利于SEO,并且在性能上也會稍微差一點,還有一些場景,使用模板引擎會更方便,比如說郵件模板。這篇文章主要討論Spring boot與模板引擎Thymeleaf、Freemaker以及JSP的集成。

一、集成Thymeleaf

第一步:引入jar包(thymeleaf對應的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ā)模式下設置為false,避免改了模板還要重啟服務器,線上設置為true,可以提高性能。

encoding&content-type:這個大家應該比較熟悉了,與Servlet中設置輸出對應屬性效果一致。

mode:這個還是參考官網的說明吧,并且這個是2.X與3.0不同,本文自動引入的包是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>序號</th>    <th>標題</th>    <th>摘要</th>    <th>創(chuàng)建時間</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還是比較簡單的,并且最大的特點就是的標簽是作為HTML元素的屬性存在的,也就是說,該頁面是可以直接通過瀏覽器來預覽的,只是沒有數據而已,這個很方便大家進行調試。

第四步 配置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,因為@RestController會自動將返回結果轉為字符串。

第五步 查看結果

Spring,Boot,模板引擎

二、Spring boot與Freemarker的集成

1、引入jar包(Freemarker對應的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外,其他的配置選項和thymeleaf類似。settings會對freemarker的某些行為產生影響,如日期格式化,數字格式化等,感興趣的同學可以參考官網提供的說明: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>序號</th>        <th>標題</th>        <th>摘要</th>        <th>創(chuàng)建時間</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、訪問頁面:

Spring,Boot,模板引擎

三、Sring boot與JSP集成:

在正式的項目開發(fā)中,現在已經極少用jsp模板了,所以Spring boot對jsp的支持也不是很好,因此配置起來比thymeleaf和Freemaker相對來說就更復雜一點。

第一步 引入jar包:

<dependency>  <groupId>javax.servlet</groupId>  <artifactId>jstl</artifactId></dependency><dependency>  <groupId>org.apache.tomcat.embed</groupId>  <artifactId>tomcat-embed-jasper</artifactId></dependency>

第一個jstl的依賴用于支持el表達式,第二個依賴用于支持jsp。注意,如果是在外部的tomcat中運行,需要將scope設置為provide,防止jar包沖突。

第二步 手動創(chuàng)建webapp目錄:

需要手動在main目錄下創(chuàng)建一個webapp的目錄,結構如下:

Spring,Boot,模板引擎

第三步 jsp路勁配置:

在application.yml中添加如下配置:

spring: mvc:  view:   prefix: /WEB-INF/jsp/   suffix: .jsp

了解Spring mvc的應該很熟悉上面的配置。

第四步 編寫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";  }

第六步 訪問結果頁面:

Spring,Boot,模板引擎

四、總結

總體來講,Spring boot對thymeleaf和Freemaker支持比較友好,配置相對也簡單一點,在實際的開發(fā)中,大多也以這兩種模板引擎為主,很少有用jsp的,jsp現在可能更多是在實驗或者學習階段使用。jsp配置比較麻煩一點的事情是不像前兩者,網上的說法基本一致,但是對Jsp的配置有很多種說法,比如說是不是需要將jar包改成war包?jsp的依賴是否需要設置為provide等等,這個主要依賴于你是否最后要將程序部署到外部的tomcat還是直接運行jar?因為本文都是直接在idea下直接運行Application類,所以這些操作就不需要了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 观塘区| 平遥县| 高尔夫| 广州市| 伊宁县| 正阳县| 五河县| 望江县| 浪卡子县| 麻栗坡县| 建阳市| 津市市| 高碑店市| 二手房| 克东县| 屏东县| 茌平县| 嘉鱼县| 台前县| 深州市| 清远市| 油尖旺区| 漯河市| 衡山县| 东方市| 临泽县| 额济纳旗| 富锦市| 上虞市| 上林县| 广饶县| 汉川市| 竹溪县| 会昌县| 同江市| 中方县| 西林县| 云阳县| 巴塘县| 乳源| 祁连县|