基于java代码的Freemarker语法和使用。

public class FreemarkerUtil {

  /**
   * 根据参数格式化字符串
   */
  public static String format(String templateContent, Map<String, Object> param) {
    Configuration cfg = new Configuration();
    StringTemplateLoader stringLoader = new StringTemplateLoader();
    stringLoader.putTemplate("myTemplate", templateContent);
    cfg.setTemplateLoader(stringLoader);
    try {
      Template template = cfg.getTemplate("myTemplate", "utf-8");
      StringWriter writer = new StringWriter();
      try {
        template.process(param, writer);
        return writer.toString();
      } catch (TemplateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return null;
  }

  public static void main(String[] args) {
    Map root = new HashMap();
    root.put("name", "javaboy2012");
    String templateContent = "欢迎:${name}";
    String testIf = "<#if name??> ${name} </#if> ,你好";
    System.out.println(FreemarkerUtil.format(templateContent, root));
    System.out.println(FreemarkerUtil.format(testIf,root));
    List<String> lists = new ArrayList<>();
    lists.add("1:a");
    lists.add("2:b");
    lists.add("3:c");
    root.put("lists",lists);
    String tempList = "<#list lists as str>${str}</#list>";
    System.out.println(FreemarkerUtil.format(tempList,root));
    //Map集合
    Map<String,String> map = new HashMap<>();
    map.put("s1","panghl1");
    map.put("s2","panghl2");
    root.put("map",map);
    String tempMap1 = "${map.s1} / ${map.s2}";
    System.out.println(FreemarkerUtil.format(tempMap1, root));
    String tempMap2 = "<#list map?keys as k>${map[k]}</#list>";
    System.out.println(FreemarkerUtil.format(tempMap2,root));

    //List<Map>集合
    List<Map<String,String>> mapList = new ArrayList<>();
    mapList.add(map);
    root.put("mapList",mapList);
    String tempMapList1 = "<#list mapList as ml>mapList: ${ml.s1} / ${ml.s2}</#list>";
    System.out.println(FreemarkerUtil.format(tempMapList1,root));
    String tempMapList2 = "<#list mapList as ml><#list ml?keys as k>mapList-->${ml[k]}</#list></#list>";
    System.out.println(FreemarkerUtil.format(tempMapList2,root));

    //获得当前迭代的索引  用lists示例
    String tempIndex = "<#list lists as l>${l_index}</#list>";
    System.out.println(FreemarkerUtil.format(tempIndex,root));

    String tempTemplate = "\n" +
            "<#assign x=\"${world}\" />\n" +
            "${x}\n" +
            " \n" +
            "<#assign x>\n" +
            "   <#list [\"星期一\", \"星期二\", \"星期三\", \"星期四\", \"星期五\", \"星期六\", \"星期天\"] as n>\n" +
            "      ${n}\n" +
            "   </#list>\n" +
            "</#assign>\n" +
            "${x}";
    root.put("world","hello panghl");
    System.out.println(FreemarkerUtil.format(tempTemplate,root));

    //日期模板
    String tempDate = "${cur_date?date}";
    root.put("cur_date", LocalDateTime.now());
    System.out.println(FreemarkerUtil.format(tempDate,root));

  }

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值