zoukankan      html  css  js  c++  java
  • freemarker模板引擎技术

    freemarker模板引擎技术:

      先上个图片

      

        简单说就是莫办系统,为了减少重复的代码,更能降低数据库的压力,

      优点:速度快,分担高并发,降低服务器压力

      缺点:使用的局限性,应用于模板固定的新闻页面,商品详情页面等

      文件后缀:XXX.ftl

      使用到的技术: IO 流 ->文件的读取->输出生成的文件

    使用步骤:

      1.创建一个Configuration 对象 直接new一个对象.构造方法的参数就是freemaker对应的版本号

      Configuration configuration = new Configuration();

      2.设置模板文件使用的字符集.一般就是UTF-8 (这一步根据情况是设定)

      3.设置模板文件所在的路径

      configuration.setDirectoryForTemplateLoading(new File("C:\Demo\pyg_project\freemarkerDemo\src\main\resources\ftl"));

      4.加载一个模板,创建一个模板对象

      Template template =
    configuration.getTemplate("文件名.ftl");

      5.创建一个模板使用的数据库集 一般是Map

      Map<String, Object> rootMap = new HashMap<String, Object>();
    //测试传入假数据
    rootMap.put("name", "张三");
    rootMap.put("message", "欢迎来到品优购商城");

      6.创建一个Writer对象(这就是上面说的io流技术)指定生成的文件名->输出

      
        Writer out = new FileWriter(new File("生成的文件名.html生成的文件类型"));

      7.调用模板对象的process方法输出文件

      template.process(rootMap,out);

      8.关闭流

       out.close();

    以上是生成的代码:

      需要的依赖

    <dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.23</version>
    </dependency>

    技术Demo及代码源码:自己手写的
      
    package cn.itcast.core;

    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.Writer;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;

    public class TestFreemarker {
    public static void main(String[] args) throws IOException, TemplateException {
    //1.创建freemarker初始化对象
    Configuration configuration = new Configuration();
    //2.加载模板的所在目录位置
    configuration.setDirectoryForTemplateLoading(new File("C:\Demo\pyg_project\freemarkerDemo\src\main\resources\ftl"));

    //3.根据模板名称创建模板对象
    Template template =
    configuration.getTemplate("test.ftl");

    //4.创建模拟的假数据
    Map<String, Object> rootMap = new HashMap<String, Object>();
    //测试传入假数据
    rootMap.put("name", "张三");
    rootMap.put("message", "欢迎来到品优购商城");
    //5.定义输出流 指定生成后的页面的位置极其名称

    ArrayList<String> numberList = new ArrayList<String>();
    numberList.add("张1");
    numberList.add("张2");
    numberList.add("张3");
    numberList.add("张4");
    numberList.add("张5");
    rootMap.put("numberList", numberList);

    HashMap<String, String> persionMap = new HashMap<String, String>();
    persionMap.put("001", "张三1");
    persionMap.put("002", "张三2");
    persionMap.put("003", "张三3");
    persionMap.put("004", "张三4");

    rootMap.put("persionMap", persionMap);


    //测试想模板中传入日期类型数据
    rootMap.put("today", new Date());

    //
    rootMap.put("point",1513513233);

    Writer out = new FileWriter(new File("test.html"));

    //6.生成
    template.process(rootMap,out);
    //7.关闭流
    out.close();

    }
    }

    技术使用的ftl文件
      
    <html>
    <head>
    <meta charset="utf-8">
    <title>Freemarker入门小DEMO </title>
    </head>
    <body>
    <#--我只是一个注释-->
    ${name},你好.${message}

    <#--在页面定义变量-->
    <#assign linkman="周先生">
    联系人:${linkman}
    <#assign info = {"mobile":"1234548541633",'address':'北京市'}>
    电话 : ${info.mobile} 地址:${info.address}


    <#--引入其他页面-->
    <#include "head.ftl">


    <#--遍历集合-->
    <#list numberList as number>
    ${number}
    <#if number_index == 0>
    这是第一次循环
    <#else >这是索引号${number_index}
    </#if>
    </#list>

    <#--遍历Map集合-->
    <#list persionMap?keys as key>
    ${key} ${persionMap[key]}
    </#list>

    <#--演示展示日期类型数据-->
    当前日期:${today?date}<br>
    当前时间:${today?time}<br>
    当前日期加时间:${today?datetime}<br>
    日期格式化:${today?string("yyyy年MM月")}<br>

    <#--展示数值对象-->
    ${point}

    <#--原样展示-->
    ${point?c}


    <#--测试是否存在-->
    <#if aaa??>
    aaa变量存在
    <#else >aaa变量不存在
    </#if>

    <#--测试${aaa}是否存在 存在展示自己不存在展示!后面的-->
    ${aaa!"母鸡"}



    <#list ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"] as n>

    <#if (n_index == 1) || (n_index == 3)>
    ${n} --红色
    <#else>
    ${n} --绿色

    </#if>-
    </#list>

    </body>
    </html>

      

    以上代码的技术知识点总结:(极其重要)

      <#list

      <#if

      <#else

      

    <#--引入其他页面-->
    <#include "head.ftl">

    <#--在页面定义变量-->
    <#assign linkman="周先生">
    积少成多, 积沙成塔.
  • 相关阅读:
    怎样理解 display:none 和 visibility:hidden
    怎样设置鼠标悬浮时弹出的文字提示框内容
    怎样获取当前元素节点的语言类型
    怎样控制元素节点的是否可拖动属性
    怎样读写分配给当前元素的快捷键
    怎样获取元素节点的标签名称
    怎样查看或修改元素节点的id属性
    怎样使用js将文本复制到系统粘贴板中
    怎样创建一个子树遍历器
    怎样创建一个子节点遍历器
  • 原文地址:https://www.cnblogs.com/lei0913/p/10846843.html
Copyright © 2011-2022 走看看