freemarker的简单入门

一.Freemarker是一个模板引擎,一个基于模板生成文本输出的通用工具。

1.引用freemarker需要在eclipse上加载插件,把插件解压后




放入eclipse的plugins文件夹里面 ,然后重启eclipse




2.添加freemarker的架包到项目


3.简单的freemarker 输出,创建一个测试类

public class Test {  
    public static void main(String[] args) throws Exception {  
         //创建freemarker配置实例    
        Configuration config=new Configuration();    
        //指定模板文件存储的目录  
        config.setDirectoryForTemplateLoading(new File("tmps"));    
        //设置数据的抓取模式  
        config.setObjectWrapper(new DefaultObjectWrapper());  
        //创建数据模型    
        Map latest = new HashMap();  
        latest.put("url", "producte/greenmouse.html");  
        latest.put("name", "jiao zi");  
        //循环数组  
        Integer[]  st = {1,2,3,4,5,6,7};  
        Map map=new HashMap();    
        map.put("sex", 1);  
        map.put("latestProduct", latest);  
        map.put("user", "woshi");    
        map.put("arr", st);  
        List<User> list = new ArrayList<>();  
        User user = new User();  
        user.setId("1");  
        user.setName("zs");  
        User user2 = new User();  
        user2.setId("2");  
        user2.setName("ls");  
        list.add(user);  
        list.add(user2);  
        map.put("list", list);  
        //加载模板文件  -- 实例化模板对象  
        Template temp =config.getTemplate("index.ftl");  
        
        //生成html 输出到目标  
        Writer out =new OutputStreamWriter(System.out );    
        temp.process(map, out);    
        out.flush();   
          
    }  
      
}  


4.创建模板对象 运用list 数组名定义在前面 as 后接 临时变量名 


<html>  
    <head>  
        <title>welcome!</title>  
    </head>  
    <body>  
        <h1>Welcome ${user}!</h1><br/>  
        <p>Our latest product:</p><br/>  
        <a href="${latestProduct.url}">${latestProduct.name}</a><br/>  
        性别:<#if sex=0>男<#else>女</#if><br/>  
  
        数组结果:<br/>  
        <#list arr as tmp>  
            <#if tmp%2=0><font color='green'>${tmp}===下标${tmp_index}</font><#else><font color='yellow'>${tmp}===下标${tmp_index}</font></#if>  
            <br/>  
            <#if tmp_index=2><#break></#if>  
        </#list>  
        list结果:<br/>  
        <#list list as user>  
            ${user.id}====${user.name}=下标${user_index}  
            <br/>  
        </#list>  
          
    </body>  
</html>  

5.输出结果 



6.定义变量

<#assign age=100>  
<#assign age>12</#assign>

7.引用定义变量的ftl inport 可以取别名 include 不能取别名,只能覆盖

<#import "include2.ftl" as t>  
<#include "include.ftl">  
${age} ==== ${t.age}  

8.宏 相当于Java中的定义方法

<#-- 定义宏-->  
<#macro add p1 p2>  
    结果为:${p1+p2}  
</#macro>  
<#--调用宏-->  
<@add p1=45 p2=546/>  

9.ftl的内置函数,没带参数可以不用()
<#assign str='  hello my name is jiaozi DDDDDD '>  
取得字符串长度:${str?length}<#--相当于Java中的str.length(),?相当于.-->  
--${str}--  
去前后空格:--${str?trim}--  
转大写:--${str?upper_case}--  
转小写:--${str?lower_case}--  
第一个字母大小:--${str?cap_first}--  
取整数--${1.61543434?int}--  
取最近的3位小数--${1.61543434?float}--  

10.循环map
public class TestMapFreemarker {  
    public static void main(String[] args) throws Exception {  
         //创建freemarker配置实例    
        Configuration config=new Configuration();    
        config.setDirectoryForTemplateLoading(new File("tmps"));    
        config.setObjectWrapper(new DefaultObjectWrapper());  
        //循环map  
        Map map = new HashMap<>();  
        Map m = new HashMap<>();  
        m.put("id_1", 1);  
        m.put("id_2", 2);  
        m.put("id_3", 3);  
        m.put("id_4", 4);  
        m.put("id_5", 5);  
        map.put("myid", m);  
        //加载模板文件  -- 实例化模板对象  
        Template temp =config.getTemplate("map.ftl");  
        //生成html 输出到目标  
        Writer out =new OutputStreamWriter(System.out);    
        temp.process(map, out);    
        out.flush();   
    }  
}  

获取所有的键  
<#list myid?keys as tmp>  
    ${tmp}  
</#list>  
  
获取所有的值  
<#list myid?values as tmp>  
    ${tmp}  
</#list>  





猜你喜欢

转载自blog.csdn.net/Yang_Hui_Liang/article/details/79152195