zoukankan      html  css  js  c++  java
  • Java模板引擎

    http://freemarker.foofun.cn/

    https://freemarker.apache.org/

    https://mvnrepository.com/artifact/org.freemarker/freemarker

    常用代码

        @Test
        public void deptemp() throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
            
            String templateName = "deptemp.ftl";
            String templatePath = "d:\template";
            String fileOutPath = "d:\deptemp.txt";
            
            Map<String, Object> map = initDatamydeptemp();
            
            Configuration config = new Configuration();
            config.setDefaultEncoding("UTF-8");
            config.setDirectoryForTemplateLoading(new File(templatePath));
            Template template = config.getTemplate(templateName);
            template.process(map, new FileWriter(fileOutPath));
            
        }

        private Map<String, Object> initDatamydeptemp() {
            
            List<Emp> emps = new ArrayList<>();
            Emp emp1 = new Emp("1","liubei");
            Emp emp2 = new Emp("2","guanyu");
            emps.add(emp1);
            emps.add(emp2);
            
            List<Dept> depts = new ArrayList<>();
            Dept dept1 = new Dept("1","jishubu",emps);
            Dept dept2 = new Dept("2","caiwubu",emps);
            depts.add(dept1);
            depts.add(dept2);
            
            //创建如下的测试数据
            Map<String, Object> map = new HashMap<String, Object>();
            //列表
            map.put("depts", depts);
            map.put("id", "001");
            return map;
        }

    常用标签

    1. 单值

    <#-- 取单值 -->
    ${id}

    2. 集合

    <#-- 取集合 -->
    <#list depts as dept>
        部门ID:${dept.id} 
        部门名称:${dept.name} 
        <#-- 取复合对象 -->
        <#list dept.emps as emp>
            <#-- if判断 -->
            <#if (emp.name=='liubei')>
                员工ID:${emp.id} 
                员工名称:${emp.name} 
            </#if>
        </#list>
    </#list>
  • 相关阅读:
    Spring Boot启动时执行初始化操作三种方法分享
    springboot自定义验证传值范围
    动态数据源玩起来
    多线程之Semaphore登录限流示例
    elementui表格自定义格式实现原理???
    31 Days of Windows Phone | Day #5 System Theming
    SQL 子查询关联查询和非关联查询 性能分享
    windows phone app 发布后在市场里找不到呢。
    APP Hub 应用发布失败,请问大家都是怎么设置可以成功提交哦
    WPF:Main方法到哪里去了?
  • 原文地址:https://www.cnblogs.com/renguanyu/p/12260557.html
Copyright © 2011-2022 走看看