FreeMarker


 

 

Freemarker快速上手

  官方網站:freemarker.apache.org

  三個操作步驟:

    1、加載模板

    2、創建數據

    3、輸出

    

package com.imooc.learn;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;

public class FreeMarkerOne {

    public static void main(String[] args) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
        /**
         * 學習freemarker
         */
        //創建核心的配置對象
        Configuration config = new Configuration(Configuration.VERSION_2_3_30);
        
        //設置加載的目錄
        
        config.setClassForTemplateLoading(FreeMarkerOne.class, "");
        
        //得到模板對象
        Template t = config.getTemplate("freemarker.ftl");
        
        //創建數據
        
        Map data = new HashMap();
        
        data.put("site", "百度");
        data.put("url", "http://www.baidu.com");
        
        //輸出結果
        t.process(data, new OutputStreamWriter(System.out));
        

    }

}

 

 

package com.imooc.learn;

import java.util.Date;
import java.util.Map;

public class Computer {
    
    private String sn;//序列號
    private String model;//型號
    private int state; //狀態 1-在用 2-閑置 3-報廢
    
    private String user; //使用人
    
    private Date dop;//采購日期
    
    private Float price; //購買價格
    
    private Map info;//電腦配置信息
    
    
    public Computer() {
        
    }
    
    
    public Computer(String sn, String model, int state, String user, Date dop, Float price, Map info) {
        super();
        this.sn = sn;
        this.model = model;
        this.state = state;
        this.user = user;
        this.dop = dop;
        this.price = price;
        this.info = info;
    }


    public String getSn() {
        return sn;
    }


    public void setSn(String sn) {
        this.sn = sn;
    }


    public String getModel() {
        return model;
    }


    public void setModel(String model) {
        this.model = model;
    }


    public int getState() {
        return state;
    }


    public void setState(int state) {
        this.state = state;
    }


    public String getUser() {
        return user;
    }


    public void setUser(String user) {
        this.user = user;
    }


    public Date getDop() {
        return dop;
    }


    public void setDop(Date dop) {
        this.dop = dop;
    }


    public Float getPrice() {
        return price;
    }


    public void setPrice(Float price) {
        this.price = price;
    }


    public Map getInfo() {
        return info;
    }


    public void setInfo(Map info) {
        this.info = info;
    }
    
    
}
package com.imooc.learn;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;

public class FreeMarkerOne {

    public static void main(String[] args) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
        /**
         * 學習freemarker
         */
        //創建核心的配置對象
        Configuration config = new Configuration(Configuration.VERSION_2_3_30);
        
        //設置加載的目錄
        
        config.setClassForTemplateLoading(FreeMarkerOne.class, "");
        
        //得到模板對象
        Template t = config.getTemplate("freemarker.ftl");
        
        //創建數據
        
        Map data = new HashMap();
        
        //實例化電腦配置 Map對象
        Map info = new HashMap();
        //電腦cpu
        info.put("cpu", "i5");
        //電腦內存
        info.put("momery", "16G");
        //實例化Computer對象
        Computer c1 = new Computer("857","MacPro",1,"admin",new Date(),13000f,info);
        
        data.put("computer", c1);
        
        data.put("site", "百度");
        data.put("url", "http://www.baidu.com");
        data.put("date", new Date());
        data.put("number", 832456.8907);
        
        
        //輸出結果
        t.process(data, new OutputStreamWriter(System.out));
        

    }

}
<#--freemarker取值 -->

${site}-${url}

<#-- ?string 格式化-->
${date?string("yyyy年MM月dd日 HH:mm:ss")}

<#-- 默認值 -->
${author!"未名意志"}

${number?string("0.00") }

<#-- 對象 -->

序號:${computer.sn}
型號:${computer.model}
使用狀態:${computer.state}
使用者:${computer.user}
日期:${computer.dop?string("yyyy年MM月dd日")}

價格:${computer.price?string("0.00")}
----------------------------------------

電腦配置
    cpu:${computer.info["cpu"]}
    momery:${computer.info["momery"]}

 分支判斷

  

 

   

 

 

<#if computer.state == 1>
    使用狀態:在使用
<#elseif computer.state == 2>
    使用狀態:閑置
<#elseif computer.state == 3>
          使用狀態:廢棄        
</#if>

<#-- ?? 是否為空 -->
<#if author??>
    使用者:${computer.user}
</#if>

<#switch computer.state>
    <#case 1>
        使用狀態:在使用 <#break>
    <#case 2>
        使用狀態:閑置  <#break>
    <#case 3>
        使用狀態:廢棄  <#break>
    <#default>
        什么都沒有 <#break>            
</#switch> 

List 迭代列表

  

 

 

 

 Freemarker內建函數

  

 

             replace                                                                                words?replace("blood","*********")

  

 

   

 

   中文學習官網:freemarker.foofun.cn 來學習內建函數

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM