zoukankan      html  css  js  c++  java
  • freemarker实现页面静态化

    package com.jy.freemark.util;
    
    import freemarker.template.Configuration;
    import freemarker.template.Template;
    import freemarker.template.TemplateException;
    
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    
    public class FreeMarkerUtil {
        
        /*
            <!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
            <dependency>
                <groupId>org.freemarker</groupId>
                <artifactId>freemarker</artifactId>
                <version>2.3.28</version>
            </dependency>
        */
        private static final Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
    
        public static Template getTemplate(String name) {
            try {
                // 通过Freemaker的Configuration读取相应的ftl
                // 设定去哪里读取相应的ftl模板文件
                cfg.setClassForTemplateLoading(FreeMarkerUtil.class, "/ftl");
                // 在模板文件目录中找到名称为name的文件
                Template temp = cfg.getTemplate(name);
                return temp;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * 控制台输出
         *
         * @param name
         * @param root
         */
        public static void print(String name, Map<String, Object> root) {
            try {
                // 通过Template可以将模板文件输出到相应的流
                Template temp = getTemplate(name);
                temp.process(root, new PrintWriter(System.out));
            } catch (TemplateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 输出HTML文件
         *
         * @param name
         * @param root
         * @param outFile
         */
        public static  void genFile(String name, Map<String, Object> root, String outFile) {
            FileWriter out = null;
            try {
                // 通过一个文件输出流,就可以写到相应的文件中,此处用的是绝对路径
                File file = new File("C:/" + outFile);
                File parentFile = file.getParentFile();
                if(!parentFile.exists()){
                    parentFile.mkdirs();
                }
                if(!file.exists()){
                    file.createNewFile();
                }
                out = new FileWriter(file);
                Template temp = getTemplate(name);
                temp.process(root, out);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TemplateException e) {
                e.printStackTrace();
            } finally {
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        public static void main(String[] args) {
            Map map = new HashMap();
            //map.put("username","张三");
            map.put("gender","男");
            map.put("date","2019-02-15 13:43:24");
            FaTianShiItem item = new FaTianShiItem();
            item.setBrief("简要说明");
            map.put("item",item);
            map.put("number",12313);
            map.put("word","english3");
            map.put("date2",new Date());
            //print("one.ftl",map);
            genFile("one.ftl",map,"one.html");
        }
    
    }

     使用到的 one.ftl模板示例

    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</title>
    </head>
    
    <body>
    <h1>你好: 姓名${username?if_exists},性别:${gender},日期:${date},简要说明:${item.brief}</h1>
    数字:${number?string.number}
    </br>
    价格:${number?string.currency}
    </br>
    百分数:${number?string.percent}
    </br><#if username??>用户名存在</#if>
    
    </br><#if username?? && username == '张三'>用户名存在且当前用户是张三</#if>
    </br><#assign flag = 123456 /><#if flag gt 10>flag大于10,flag=${flag}</#if>
    </br>截取字符串,包头不包尾(下标):${word?substring(0,3)}
    </br>是否包含目标字符串:${word?contains('english')?string}
    </br>转换成日期格式1:${date?date('yyyy-MM-dd')}
    </br>转换成日期格式3:${date?datetime('yyyy-MM-dd HH:mm:ss')}
    </br>字符串长度:${word?length}
    </br>存在则首字母大写:${word?cap_first}
    </br>字母小写:${word?lower_case}
    </br>字母大写:${word?upper_case}
    </br>java:new Date转化为时间字符串:${date2?string('yyyy-MM-dd HH:mm:ss')}
    </br>java:new Date:${date2?string('yyyy-MM-dd')}
    
    </body>
    </html>
    人生没有彩排,每天都是现场直播!
  • 相关阅读:
    21. 斐波那契数列
    22. 旋转数组的最小数字
    php图像处理链接
    FileOprSer.class.php(文件上传与下载类)
    SqlHelper.class.php+分页类方法
    file写入方式 和copy
    file 读取方式
    pytest+allure+jenkins 持续集成平台生成allure报告
    memcached cas操作
    php-fpm
  • 原文地址:https://www.cnblogs.com/northern-light/p/10497467.html
Copyright © 2011-2022 走看看