• freemark


    1. 什么是freemark  

          Freemaker是一个”模板引擎”,也可以说是一个基于模板技术的生成文本输出的一个通用工具

    2.一般的用途:

    l 能用来生成任意格式的文本:HTML,XML,RTF,JAVA源码,等等.

    3.基本目标:代替以前的jsp显示页面 

            注意一般用freemarke来做哪些jsp页面:

                    1. 多查询页面

                    2.操作少的页面只是单纯的展示 比如新闻页面 

                    3.访问量比较大的网站

       优点 : 访问速度快 效率高 

                 每次查询不进入数据库 缓解数据库压力 

                 防止前台的高并发

    JSP编译后的class代码的位置
    D:WorkspaceExamsix.metadata.pluginsorg.eclipse.wst.server.core mp1
     
    而使用freemarker 则不用生成class文件 

       freemark是需要jar包的

         freemerk是要导入jar包的

        导入freemark的jar 包 一下用的是 2.3.16.jar 包   有文件

    maven : 坐标

          <dependency>

                  <groupId>org.freemarker</groupId>

                  <artifactId>freemarker</artifactId>

                   <version>2.3.16</version>

         </dependency>

    第一个简单例子:

             控制台输出

    //1、创建数据模型 

    Map<String,Object> root = new HashMap<String,Object>();

    //2、为数据模型添加值  

    root.put("user", "橙子");

    root.put("url", "www.baidu.com");

    root.put("name", "王博");

    //3.指定一个你要使用的模板名字   模型的名字不一定是 .ftl 文件  

    String name="tempalte_news.shtml";

    //4.通过Freemaker的Configuration读取相应的ftl文件 用的是template 接受模板

    Configuration cfg = new Configuration();

    //设定去哪里读取相应的ftl模板文件

    cfg.setClassForTemplateLoadin

    uiuuug(TestYang.class,"../template")

    //然后在模板文件目录中找到名称为name的文件   这样一个模板就找到了 temp

    Template temp = cfg.getTemplate(name);

    //5.最后一步 把模板和你的数据模板处理加工一下 就是说融合一下 并且输出

    temp.process(root, new PrintWriter(System.out));

    // 需要抛异常的!!!!

    生成的静态页面

    public void fprint(String name,Map<Object,Object> rootList,String outFile) {

    //name 是要使用的模板名称  rootList 是数据模型  注意事项: 必须是Map 

    // outFile 是要生成的静态化页面

    FileWriter out = null;

    try {

    //通过一个文件输出流FileWriter,就可以写到相应的文件中

    //获取文件得保存路径

      String rootPath=getClass().getResource("/").getFile().toString();  

      rootPath = rootPath.replace("/build/classes", "/WebContent/WEB-INF");

     System.out.println(rootPath);

    //实例化文件输出流

    out = new FileWriter(new File(rootPath+"html/"+outFile));

    //根据名字查询索要的模板

    Template temp = this.getTemplate(name);

    // temp是使用的模板  root为数据模板   out是写出的文件物理地址  temp 

    temp.process(rootList, out);

    System.out.println(outFile+"生成成功!!!");

    catch (IOException e) {

    e.printStackTrace();

    catch (TemplateException e) {

    e.printStackTrace();

    finally {

    try {

    if(out!=null) out.close();

    catch (IOException e) {

    e.printStackTrace();

    }

    }

    }

    模板:

    <#list rootList as user>

           <tr>

            <td>${user.userId}</td>

            <td>${user.userName}</td>

            <td>${user.userAccount}</td>

            <td>${user.userPw}</td>

            <td>

            <#if user.userSex == 1>

              男

            <#elseif user.userSex == 2>

            女

            <#else>

            没选

            </#if>

            </td>

            <td>删除</td>

           </tr>

           </#list>


    注意事项:

             在显示时使用的list 循环标签 其实里面循环的是Map

    freemerk 标签!!!

    循环迭代标签

    list 和 <#break>

            //sequence 要循环的属性   好比c 标签的里面的 items 

            // item 是你循环后的属性名类似c 标签里面的 var 

           //注意事项: sequence 是map里的键 

            <#list sequence as item>   
                     
            </#list>

            <#break>  //跳出list循环

    实例:

            <#list users as user>

                ${user.id}---------${user.name}-------${user.age}<br/>

            </#list>

    </#list> 其他属性:

                       item_index:是list当前值的下标 
                    item_has_next:判断list是否还有值

    <#if 判断条件>   

    <#if x = 1>   

    </#if>   

    <#if 判断条件> 和<#else 判断条件>
    <#if x = 1>   
      x is 1  
    <#else>   
      x is not 1  
    </#if>

    <#if><#else if ><#else> 联合使用

     <#if  x == 1>

                  x is 1

       <#else x == 2>

                 x is 2

       <#else>

                 x not 1 or 2 

       </#if>

    日期转换

    <#--日期也不能直接输出,需要转换为字符串String-->

    注意事项:<#--${now?string}没有为日期设定格式也会报错-->

    ${值?string("yyyy-MM-dd HH:mm:ss")}

    实例:

    用户生日: 

    ${user.userBir?string("yyyy-MM-dd")}

    声明和定义  

    定义:<#assign 属性='值'>;

    取${属性}

    实例:

    <#--定义变量-->

    <#assign username="王博"/>

    ${username}

  • 相关阅读:
    Message高级特性 & 内嵌Jetty实现文件服务器
    springboot中使用kindeditor富文本编辑器实现博客功能&vue-elementui使用vue-kindeditor
    Embarcadero RAD Studio XE5
    经典营销故事
    百度竞价教程 借助百度热力图让你的效果翻10倍
    无本借力:他是如何实现年收入70万?
    不用软件快速拥有几百个QQ群并都是管理员
    质保、保修、包修:含义不同
    域名反向解析在自建邮件群发服务器中的应用
    2014年1月1日,马年
  • 原文地址:https://www.cnblogs.com/cdeelen/p/11209530.html
Copyright © 2020-2023  润新知