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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Freemarker使用

發布時間:2025/3/20 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Freemarker使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

添加jar包

<!-- freemarker --> <dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><freemarker.version>2.3.23</freemarker.version> </dependency>

使用步驟

第一步:創建一個Configuration對象,直接new一個對象
構造方法的參數就是freemarker對于的版本號
第二步:設置模板文件所在的路徑
第三步:設置模板文件使用的字符集。一般就是utf-8
第四步:加載一個模板,創建一個模板對象
第五步:創建一個模板使用的數據集
可以是pojo也可以是map,一般是Map
第六步:創建一個Writer對象
一般創建一FileWriter對象,指定生成的文件名
第七步:調用模板對象的process方法輸出文件
第八步:關閉流

public class TestFreeMarker {@Testpublic void testFreemarker() throws Exception {//1.創建一個模板文件//2.創建一個Configuration對象Configuration configuration = new Configuration(Configuration.getVersion());//3.設置模板所在的路徑configuration.setDirectoryForTemplateLoading(new File("D:/workspaces-itcast/JavaEE28/taotao-item-web/src/main/webapp/WEB-INF/ftl"));//4.設置模板的字符集,一般utf-8configuration.setDefaultEncoding("utf-8");//5.使用Configuration對象加載一個模板文件,需要指定模板文件的文件名。 // Template template = configuration.getTemplate("hello.ftl");Template template = configuration.getTemplate("student.ftl");//6.創建一個數據集,可以是pojo也可以是map,推薦使用mapMap data = new HashMap<>();data.put("hello", "hello freemarker");Student student = new Student(1, "小米", 11, "北京昌平回龍觀");data.put("student", student);List<Student> stuList = new ArrayList<>();stuList.add(new Student(1, "小米", 11, "北京昌平回龍觀"));stuList.add(new Student(2, "小米2", 12, "北京昌平回龍觀"));stuList.add(new Student(3, "小米3", 13, "北京昌平回龍觀"));stuList.add(new Student(4, "小米4", 14, "北京昌平回龍觀"));stuList.add(new Student(5, "小米5", 15, "北京昌平回龍觀"));stuList.add(new Student(6, "小米6", 16, "北京昌平回龍觀"));stuList.add(new Student(7, "小米7", 17, "北京昌平回龍觀"));data.put("stuList", stuList);//日期類型的處理data.put("date", new Date());data.put("val","123456");//7.創建一個Writer對象,指定輸出文件的路徑及文件名。Writer out = new FileWriter(new File("D:/temp/javaee28/out/student.html"));//8.使用模板對象的process方法輸出文件。template.process(data, out);//9.關閉流out.close();} }

語法格式

根據map中的key,獲取數據
模板字段與key一致
${key}

總結

以上是生活随笔為你收集整理的Freemarker使用的全部內容,希望文章能夠幫你解決所遇到的問題。

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