日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

freemarker模板最小案例实现

發布時間:2025/3/19 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 freemarker模板最小案例实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

freemarker是什么

百度百科:

FreeMarker是一款模板引擎: 即一種基于模板和要改變的數據, 并用來生成輸出文本(HTML網頁、電子郵件、配置文件、源代碼等)的通用工具。 它不是面向最終用戶的,而是一個Java類庫,是一款程序員可以嵌入他們所開發產品的組件。

FreeMarker是免費的,基于Apache許可證2.0版本發布。其模板編寫為FreeMarker Template Language(FTL),屬于簡單、專用的語言。需要準備數據在真實編程語言中來顯示,比如數據庫查詢和業務運算, 之后模板顯示已經準備好的數據。在模板中,主要用于如何展現數據, 而在模板之外注意于要展示什么數據?[1]?。

依賴

使用spring boot開發,增加如下依賴:

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency> </dependencies>

controller:

package com.monkey.freemarker.controller;import com.monkey.freemarker.entity.Item; import freemarker.template.Configuration; import freemarker.template.Template; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import javax.xml.ws.RequestWrapper; import java.io.*; import java.math.BigDecimal; import java.util.HashMap; import java.util.Map; import java.util.Random;/*** @author :XXX* @date :Created in 2021/11/18 0018 8:34* @description:* @modified By:* @version: v1.0*/ @RestController public class ItemController {@Autowiredprivate Configuration configuration;@RequestMapping("create")public String createHtml() throws Exception {// 獲取模板Template template = configuration.getTemplate("item.tpl");// 數據填充模板Map<String, Object> dataModel = new HashMap<>();dataModel.put("item", new Item("iphone 13", new BigDecimal(10000), 10000));String path = "D:/tplHtml";File file = new File(path + "/item-" + 100 + ".html");if (file.exists()) {System.out.println("文件已存在");} else {file.createNewFile();BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));template.process(dataModel, bufferedWriter);}return "create success";} }

entity:?

package com.monkey.freemarker.entity;import java.math.BigDecimal;/*** @author :XXX* @date :Created in 2021/11/18 0018 8:34* @description:商品* @modified By:* @version: v1.0*/ public class Item {private String goodName;private BigDecimal goodPrice;private int evaluateCount;public Item(String goodName, BigDecimal goodPrice, int evaluateCount){this.evaluateCount = evaluateCount;this.goodName = goodName;this.goodPrice = goodPrice;}public String getGoodName() {return goodName;}public void setGoodName(String goodName) {this.goodName = goodName;}public BigDecimal getGoodPrice() {return goodPrice;}public void setGoodPrice(BigDecimal goodPrice) {this.goodPrice = goodPrice;}public int getEvaluateCount() {return evaluateCount;}public void setEvaluateCount(int evaluateCount) {this.evaluateCount = evaluateCount;} }

tpl:

<h1>商品詳情頁</h1> 商品展示<p>商品名稱:${item.goodName }</p><p>商品價格:${item.goodPrice }</p><p>商品評價數:${item.evaluateCount }</p>

工程目錄結構:

?效果圖:

?

總結

以上是生活随笔為你收集整理的freemarker模板最小案例实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。