激情久久久_欧美视频区_成人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

延伸 · 閱讀

精彩推薦
1183
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
主站蜘蛛池模板: 三级国产三级在线 | 久久精品亚洲欧美日韩精品中文字幕 | 中国黄色一级生活片 | 92自拍视频 | 九九黄色 | 91在线免费观看 | 国产一区二区三区色淫影院 | 成人免费av在线播放 | 亚洲影视中文字幕 | 国产自在线 | 欧美伦交 | 鲁久久| 极品xxxx欧美一区二区 | 日日鲁一鲁视频 | 久久精片 | free japan xxxxhdsex69 | 成人免费观看毛片 | 国产一级免费电影 | 在线a免费观看 | 午夜视频播放 | 新久草在线视频 | 国产精品视频久久久 | 一级黄色大片在线观看 | 欧美一级片一区 | 麻豆视频在线播放 | 欧美一级高清片_欧美高清aa | 在线成人一区二区 | 久久av一区二区 | 成人免费自拍视频 | 麻豆国产网站 | 久久av免费观看 | 日日操夜夜透 | 在线视频成人永久免费 | 欧美一区二区三区久久精品视 | 李宗瑞国产福利视频一区 | 色av成人天堂桃色av | 黄色免费不卡视频 | 欧美人的天堂一区二区三区 | 一级色毛片| 欧美精品久久久久久久久老牛影院 | 精品久久中文网址 |