freemarker

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>  

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值