zoukankan      html  css  js  c++  java
  • [转]Freemarker数据类型转换

    转至: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填充为“”空字符串;这样,如果在插值处要调用内置函数,就应该先使用<#if标签先判断是否存在了;布尔型、日期型默认不会自动转换为字符串,需要内置函数处理,在调用内置函数前要做是否存在的验证;最后,以上测试,日期型类型转换是根据Date类型来转换的,string内置函数可以比较灵活地使用自定义格式显示;

  • 相关阅读:
    header
    panel----单个基础版
    vue-demo
    js不同类型变量比较
    reset.css
    关于各个浏览器的兼容问题
    git
    AMD与CMD区别
    喜欢前端的看过来哦
    js中数组去重的几种方法
  • 原文地址:https://www.cnblogs.com/luoxiaolei/p/5331742.html
Copyright © 2011-2022 走看看