freemarker

来源:互联网 发布:极有家 知乎 编辑:程序博客网 时间:2024/06/12 20:18

1.创建一个freemarker实例

Configuration cfg = new Configuration();//配置ftl查找目录cfg.setDirectoryForTemplateLoading(        new File("template"));//设置数据的抓取模式cfg.setObjectWrapper(new DefaultObjectWrapper()); //构建数据Map root = new HashMap();root.put("user", "Big Joe");Map latest = new HashMap();root.put("latestProduct", latest);latest.put("url", "products/greenmouse.html");latest.put("name", "green mouse");latest.put("sex", 1);String[] strArray = new String[]{"1","2","3","4","5","6"};root.put("str", strArray);//实例化模板对象Template temp = cfg.getTemplate("testinclude.ftl");//生成html 输出到目标User user = new User();user.setAge(18);user.setUsername("波老师");User user1 = new User();user1.setAge(20);user1.setUsername("仓老师");List list = new ArrayList<>();list.add(user);list.add(user1);root.put("userList", list);FileOutputStream fos = new FileOutputStream("C:\\html\\a.html");Writer out = new OutputStreamWriter(System.out);temp.process(root, out);out.flush();  


2.ftl模板如下

<html><head>  <title>Welcome!</title></head><body>  <h1>Welcome ${user}!</h1>  <p>Our latest product:  <a href="${latestProduct.url}">${latestProduct.name}</a>!
输出性别:  <#if latestProduct.sex=2>  男  <#else>  女  </#if>
输出结果:  <#list str as tmp>  <#if tmp_index==2><#break></#if>  <#if (tmp_index+1)%2==0>  <p style="background:green;">${tmp_index}--${tmp}</p>  <#else>  <p style="background:red;">${tmp_index}--${tmp}</p>  </#if>  </#list>  <br/>
输出集合:  <#list userList as u>${u.age}--${u.username}  </#list></body></html>

3.include import 的区别

include 调用同名变量 会覆盖
<#include "include.ftl"><#include "include2.ftl">${age}

import 可以设置别名,分别调用
<#import "include.ftl" as c1><#import "include2.ftl" as c2>${c2.age} ${c1.age}

3.macro 的使用

宏可以自定义函数
<#macro mymacro p1 p2>${p1+p2}</#macro><@mymacro p1=234 p2=234 />

4.freemarker 常用的函数

  1. <#assign str="">  
  2. <#--常见 字符串函数 -->  
  3. ${"asddfdsd"?substring(3,5)} <#-- 从索引为3位置开始截取,到索引为5位置结束不包括5 5-3-->  
  4. ${"abcd"?cap_first}<#--字符串的第一个字符大写-->  
  5. ${"abcd"?uncap_first}<#--字符串的第一个字符小写-->  
  6. ${"abcd say "?capitalize}<#--字符串的 没个字母的第一个字符大写-->  
  7. ${"abcd"?ends_with("a")?c}<#--判断 字符串的最后一个字符是什么,返回的是布尔值 需要特殊处理-->  
  8. ${"abcd"?starts_with("a")?c}<#--判断 字符串的第一个字符是什么,返回的是布尔值 需要特殊处理-->  
  9. ${"abcaad"?index_of("a")}<#--判断 指定字符第一次出现的索引位置-->  
  10. ${"abcda"?last_index_of("a")}<#--判断 指定字符最后一次出现的索引位置-->  
  11. ${"abcd"?length}<#--返回字符串的长度-->  
  12. ${"abcd"?left_pad(5,"1x")}<#--指定字符串的长度5 若字符串的长度<5 则向字符串的左侧插入指定的字符串,默认插入空格-->  
  13. ${"abcd"?right_pad(5,"1x")}<#--指定字符串的长度5 若字符串的长度<5 则向字符串的右侧插入指定的字符串,默认插入空格-->  
  14. ${"abcd"?contains("ac")?c}<#--判断字符串是否包含指定字符串 返回布尔值 需要处理-->  
  15. ${"abcd"?replace("a","A")}<#--替换指定字符串-->  
  16. <#assign s="abcd"?split("c")><#-- 分割字符串-->  
  17. <#list s as seq>  
  18.     ${seq}  
  19. </#list>  
  20. ${"   v abcd cc "?trim}<#--字符串去除字符串两端的空格-->  
  21. ${"   v abcd cc "?trim}<#--字符串去除字符串两端的空格-->  
  22. <#list " we are chinese you no diao"?word_list as word><#--以单词的 形式 分割字符串-->  
  23.     ${word}  
  24. </#list>  

原创粉丝点击