[轉]Freemarker資料類型轉換

來源:互聯網
上載者:User

標籤:

轉至:http://blog.sina.com.cn/s/blog_667ac0360102eaz8.html

// 測試程式

package myTest;

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class FtlTest {

 public static void main(String[] args) throws Exception {
  Map<String, Object> dataMap = getModel_1();
  Configuration configuration = new Configuration();
  configuration.setDefaultEncoding("utf-8");
  configuration.setClassForTemplateLoading(FtlTest.class, "/ftl/");
  Template template = configuration.getTemplate("test.ftl");
  template.process(dataMap, new BufferedWriter(new OutputStreamWriter(System.out)));
  dataMap = getModel_2();
  template.process(dataMap, new BufferedWriter(new OutputStreamWriter(System.out)));
  dataMap = getModel_3();
  template.process(dataMap, new BufferedWriter(new OutputStreamWriter(System.out)));
 }
 
 public static Map<String, Object> getModel_1() {
  Map<String, Object> dataMap = new HashMap<String, Object>();
  dataMap.put("data_string", "string");
  dataMap.put("data_int", 3);
  dataMap.put("data_float", 3.333);
  dataMap.put("data_boolean", true);
  dataMap.put("data_date", new Date());
  return dataMap;
 }
 public static Map<String, Object> getModel_2() {
  Map<String, Object> dataMap = new HashMap<String, Object>();
  return dataMap;
 }
 public static Map<String, Object> getModel_3() {
  Map<String, Object> dataMap = new HashMap<String, Object>();
  SimpleDateFormat df = new SimpleDateFormat("yyyy");
  Date year = null;
  try {
   year = df.parse("2013");
  } catch (ParseException e) {
   throw new RuntimeException(e);
  }
  dataMap.put("data_date", year);
  return dataMap;
 }
}

// 模板

<#escape x as x?default("")>
資料類型測試
data_string=${data_string}
data_int=${data_int}
data_float=${data_float}
data_boolean=<#if data_boolean?exists>${data_boolean?string}</#if>
data_date=<#if data_date?exists>${data_date?string(‘yyyy-MM-dd‘)}</#if>
</#escape>

 

// 輸出

資料類型測試
data_string=string
data_int=3
data_float=3.333
data_boolean=true
data_date=2013-09-25
資料類型測試
data_string=
data_int=
data_float=
data_boolean=
data_date=
資料類型測試
data_string=
data_int=
data_float=
data_boolean=
data_date=2013-01-01


 

說明:freemarker不會處理null資料,null會報錯;使用<#escape標籤,來為所有的插值做轉換;轉換調用了default內建函數,將null填充為“”Null 字元串;這樣,如果在插值處要調用內建函數,就應該先使用<#if標籤先判斷是否存在了;布爾型、日期型預設不會自動轉換為字串,需要內建函數處理,在調用內建函數前要做是否存在的驗證;最後,以上測試,日期型類型轉換是根據Date類型來轉換的,string內建函數可以比較靈活地使用自訂格式顯示;

[轉]Freemarker資料類型轉換

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.