溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何在SpringBoot中整合freemarker

發布時間:2021-06-08 17:17:44 來源:億速云 閱讀:230 作者:Leah 欄目:編程語言

這篇文章給大家介紹如何在SpringBoot中整合freemarker,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

項目文件目錄:

如何在SpringBoot中整合freemarker

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

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

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

#  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):

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8"/>
  <title></title>
</head>
<body>
FreeMarker模板引擎
<h2>${resource.name}</h2>
<h2>${resource.website}</h2>
<h2>${resource.language}</h2>
</body>
</html>

我們在resources下新建resource.properties:

com.haozz.opensource.name=wangshu
com.haozz.opensource.website=www.haozz.top:18158/
com.haozz.opensource.language=chinese

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

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類:

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中新增方法:

@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:

<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已經初始化了。

關于如何在SpringBoot中整合freemarker就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女