国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringBoot整合freemarker的講解

SpringBoot整合freemarker的講解

2021-07-02 15:06Haozz_1994 Java教程

今天小編就為大家分享一篇關于SpringBoot整合freemarker的講解,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

freemarker和thymeleaf是模板引擎。在早前我們使用struts或者springmvc等框架的時候,使用的都是jsp,jsp的本質其實就是一個servlet,其中的數據需要在后端進行渲染,然后再在客戶端顯示,效率比較低下。而模板引擎恰恰相反,其中的數據渲染是在客戶端,效率方面比較理想一點。前后端不分離的話用模板引擎比較好,前后端分離的話其實用處并不大很大。spring官方比較推薦的是thymeleaf,其文件后綴是html。本篇文章我們主要來看看springboot整合freemarker,springboot整合thymeleaf我們將在后面的文章中講解。

先來看一下項目文件目錄:

SpringBoot整合freemarker的講解

首先,pom.xml中導入freemarker的依賴:

?
1
2
3
4
<dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-freemarker</artifactid>
</dependency>

application.properties(或yml)配置文件中加入freemarker相關配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
#  freemarker靜態資源配置
#    設定ftl文件路徑
spring.freemarker.tempalte-loader-path=classpath:/templates
#    關閉緩存,及時刷新,上線生產環境需要修改為true
spring.freemarker.cache=false
spring.freemarker.charset=utf-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl

這里指定了freemarker文件的路徑是classpath/templates,在resources文件夾下的templates新建freemarker文件夾,并且在其中新建index.ftl(上面配置文件中已經指定了freemarker模板的文件后綴為ftl):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<!doctype html>
<html>
<head lang="en">
  <meta charset="utf-8"/>
  <title></title>
</head>
<body>
freemarker模板引擎
<h1>${resource.name}</h1>
<h1>${resource.website}</h1>
<h1>${resource.language}</h1>
</body>
</html>

我們在resources下新建resource.properties:

?
1
2
3
com.haozz.opensource.name=wangshu
com.haozz.opensource.website=www.haozz.top:18158/
com.haozz.opensource.language=chinese

在springboot啟動類統計目錄下新建utils包,在其中新建resources類(此處使用配置文件引入相關數據):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.haozz.freemarkerdemo.utils;
import org.springframework.boot.context.properties.configurationproperties;
import org.springframework.context.annotation.configuration;
import org.springframework.context.annotation.propertysource;
//表示這個類是一個讀取配置文件的類
@configuration
//指定配置的一些屬性,其中的prefix表示前綴
@configurationproperties(prefix = "com.haozz.opensource")
//指定所讀取的配置文件的路徑
@propertysource(value = "classpath:resource.properties")
public class resource {
  private string name;
  private string website;
  private string language;
  //...setter and getter
}

新建controller包,新建freemarkerctrl類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.haozz.freemarkerdemo.controller;
import com.haozz.freemarkerdemo.utils.resource;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.ui.modelmap;
import org.springframework.web.bind.annotation.requestmapping;
@controller
@requestmapping(value = "/ftl")
public class freemarkerctrl {
  @autowired
  private resource resource;
  @requestmapping(value = "index")
  public string index(modelmap map){
    map.addattribute("resource",resource);
    return "freemarker/index";
  }
}

這里的modelmap就相當于springmvc中的modelandview,其中的很多方法也很類似,我們這里返回的字符串就是freemarker模板的路徑,不用寫后綴,因為配置文件中已經指定了后綴為.ftl

瀏覽器發起請求,得到結果:

SpringBoot整合freemarker的講解

這樣,springboot整合freemarker就好了。

我們再來試一下表格的形式。

freemarkerctrl中新增方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@requestmapping(value ="center")
  public string center(modelmap map){
    map.put("users",parseusers());
    map.put("title","用戶列表");
    return "freemarker/center/center";
  }
  private list<map> parseusers(){
    list<map> list= new arraylist<>();
    for(int i=0;i<10;i++){
      map map= new hashmap();
      map.put("name","kevin_"+i);
      map.put("age",10+i);
      map.put("phone","1860291105"+i);
      list.add(map);
    }
    return list;
  }

在resources/templates/freemarker下新建center文件夾,新建center.ftl:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<html lang="zh-cn">
<head>
  <meta charset="utf-8"/>
  <title>${title}</title>
  <style>
    table {
      width: 50%;
      font-size: .938em;
      border-collapse: collapse;/*邊框合并*/
    }
    th {
      text-align: left;
      padding: .5em .5em;
      font-weight: bold;
      background: #66677c;color: #fff;
    }
    td {
      padding: .5em .5em;
      border-bottom: solid 1px #ccc;
    }
    table,table tr th, table tr td { border:1px solid #0094ff; }/*設置邊框*/
  </style>
</head>
<body>
<table>
  <tr>
    <th>name</th>
    <th>age</th>
    <th>phone</th>
  </tr>
    <#list users as user>
      <tr>
        <td>${user.name}</td>
        <td>${user.age}</td>
        <td>${user.phone}</td>
      </tr>
    </#list>
</table>
</body>
</html>

瀏覽器請求:

SpringBoot整合freemarker的講解

可以看到,在center.ftl中,我們使用了<#list users as user>的寫法,這個相當于jstl表達式中的c:foreach。而users集合我們在freemarkerctrl已經初始化了。

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接

原文鏈接:https://blog.csdn.net/hz_940611/article/details/80706772

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 国产黄色一级片视频 | 亚洲黄色激情 | 欧美日韩成人在线播放 | 国产精品成av人在线视午夜片 | www.97超碰.com| 久久精品无码一区二区三区 | 夜夜爽99久久国产综合精品女不卡 | 日韩中文字幕 | av网站地址 | 亚洲网站在线 | 999精品视频 | 欧美日韩精品在线观看 | 91在线麻豆| 久久草视频 | 久久精品99视频 | 一级国产 | 欧美精品一区二区蜜臀亚洲 | 国产欧美日韩综合精品一区二区 | 中文字幕在线免费视频 | 久久精品一级毛片 | 亚洲一区中文字幕在线观看 | 国产精品欧美一区二区三区不卡 | 久久久久高清视频 | 成人精品一区二区三区中文字幕 | 久久精品免费观看 | 可以在线观看的黄色 | av一二三四| 久久久精品蜜桃 | 伊人色网| 在线观看免费黄色小视频 | 国产欧美日韩在线观看 | 国产一区不卡 | 欧美另类视频 | 操操操操操操 | 亚洲精品一区二区三区蜜桃久 | 国产欧美在线播放 | 日韩精品久久久久 | 一区免费看 | 97久久精品人人做人人爽50路 | 国产精国产精品 | 狠狠综合 |