溫馨提示×

溫馨提示×

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

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

如何在SpringBoot中整合freemarker

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

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

項目文件目錄:

如何在SpringBoot中整合freemarker

首先,pom.xml中導(dǎo)入freemarker的依賴:

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

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

#  freemarker靜態(tài)資源配置
#    設(shè)定ftl文件路徑
spring.freemarker.tempalte-loader-path=classpath:/templates
#    關(guān)閉緩存,及時刷新,上線生產(chǎn)環(huán)境需要修改為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(上面配置文件中已經(jīng)指定了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啟動類統(tǒng)計目錄下新建utils包,在其中新建Resources類(此處使用配置文件引入相關(guān)數(shù)據(jù)):

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就相當(dāng)于SpringMVC中的ModelAndView,其中的很多方法也很類似,我們這里返回的字符串就是freemarker模板的路徑,不用寫后綴,因為配置文件中已經(jīng)指定了后綴為.ftl

瀏覽器發(fā)起請求,得到結(jié)果:

如何在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; }/*設(shè)置邊框*/
  </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>的寫法,這個相當(dāng)于jstl表達式中的c:forEach。而users集合我們在FreeMarkerCtrl已經(jīng)初始化了。

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

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI