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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

freeMarker自定义函数的使用

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

1. 編寫函數

?

1 package com.freemarker.test;
2
3 ?import java.text.SimpleDateFormat;
4 ?import java.util.Date;
5 import java.util.List;
6 import freemarker.template.TemplateMethodModel;
7 import freemarker.template.TemplateModelException;
8
9
10 public class SqlGetSysdateMethod implements TemplateMethodModel
11 {
12
13
14 public Object exec( List args ) throws TemplateModelException
15 {
16 //得到函數第一個參數,得到的字符串兩頭會有引號,所以replace
17 String datePattern=(args.get( 0 ).toString()).replace( "'", "" );
18
19 Date date = new Date();
20 SimpleDateFormat sdf =new SimpleDateFormat(datePattern);
21
22 return sdf.format( date );
23 }
24
25 } 附上一個官方文檔鏈接:http://freemarker.sourceforge.net/docs/ref_builtins.html

?

2.注冊與使用

  有兩種方式:

  (1).在模板文件中注冊,在模板中使用

  <#assign getSysdate= "package?com.freemarker.test.SqlGetSysdateMethod"?new()>

  <#assign curdate=?getSysdate("yyyy-MM-dd"t)/>


  ?(2).處理模板文件時注冊
    關鍵代碼:


Map
<String,Object> root=new HashMap<String, Object>();
root.put(
"getSysdate", new StringLengthMethod());

Configuration config
=new Configuration();

File file
=new File(templatePath);
//并加載模板文件
config.setDirectoryForTemplateLoading(file);
//設置包裝器,并將對象包裝為數據模型
config.setObjectWrapper(new DefaultObjectWrapper());

//獲取模板,并設置編碼方式,這個編碼必須要與頁面中的編碼格式一致
Template template=config.getTemplate(templateName,templateEncoding);
//合并數據模型與模板

template.process(root, out);

?

轉載于:https://www.cnblogs.com/ashu123/archive/2011/06/29/freeMarker_0.html

總結

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

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