网页静态化Freemarker入门

为什么要网页静态化?

    网页静态化技术和缓存技术的共同点都是为了减轻数据库的访问压力,但是具体的应用场景不同,

 缓存比较适合小规模的数据,而网页静态化比较适合大规模且相对变化不太频繁的数据

 另外网页静态化还有利于SEO

 

 

   模型  +   数据模型  通过freemarker 输出静态页面

 

入门小demo

  1.导入依赖   

      <groupId>org.freemarker</groupId>

 

      <artifactId>freemarker</artifactId>

 

      <version>2.3.23</version>

      2.程序编写

public class FreemakerDemo1 {
    public static void main(String[] args) throws IOException, TemplateException {
        Configuration configuration =new Configuration(Configuration.getVersion()); //获取configuration对象
//指定模板文件夹所在位置 configuration.setDirectoryForTemplateLoading(
new File("E:\\IdeaProjects\\freemaker_start_demo\\src\\main\\resources\\")); configuration.setDefaultEncoding("utf-8"); //指定字符集 Template template =configuration.getTemplate("test.ftl"); //指定模板名称 Map map = new HashMap(); //数据模型容器,一般为Map 或者pojo,推荐使用Map map.put("name","张无忌" ); map.put("message","欢迎来到召唤师峡谷" ); map.put("success",true); List goodsList=new ArrayList(); Map goods1=new HashMap(); goods1.put("name", "苹果"); goods1.put("price", 5.8); Map goods2=new HashMap(); goods2.put("name", "香蕉"); goods2.put("price", 2.5); Map goods3=new HashMap(); goods3.put("name", "橘子"); goods3.put("price", 3.2); goodsList.add(goods1); goodsList.add(goods2); goodsList.add(goods3); map.put("goodsList", goodsList); map.put("today",new Date()); map.put("point",120023412); Writer out = new FileWriter(new File("d:\\test.html")); //创建输出流,指定生成html的 地址 和 名称 template.process(map,out);//执行生成操作 out.close(); } }

   模板

<html>
<head>
    <meta charset="utf-8">
    <title>Freemarker入门小DEMO </title>
</head>
<body>
<#include "head.ftl">  <#--引入其他ftl文件-->
<#--我只是一个注释,我不会有任何输出  -->
${name},你好。${message}
<br/>
<#assign linkman="张先生">
联系人:${linkman}
<br/>
<#assign info={"mobile":"13333333","address":"西安市"}>
电话:${info.mobile} 地址:${info.address}

<br/>
<br/>
<br/>
<#if success=true>
   你成功了
    <#else >
   你失败了
</#if>

<br/>
-----水果------<br/>
<#list goodsList as goods>
    ${goods_index} 商品名称: ${goods.name} 价格:${goods.price} <br/>
</#list>
共${goodsList?size} 种
<br/>
<br/>
<br/>
<#assign text="{'name':'张飞','score':'300'}">
<#assign data = text?eval>
名字:${data.name} <br/>
分数:${data.score}
<br/>
当前日期:${today?date}<br/>
当前时间:${today?time}<br/>
当前时间+日期:${today?datetime}<br/>
日期格式化:${today?string("yyyy年MM月dd日")}
<br/>
<br/>
<br/>
累计积分:${point}
<br/>
累计积分2:${point?c}<#--转字符串-->

<br/>
<br/>
<#if aaa??>
    aaa变量存在
<#else >
   aaa不存在
</#if>

<br/>
${aaa!'bbb'}

</body>
</html>

生成的静态html

 

<html>
<head>
    <meta charset="utf-8">
    <title>Freemarker入门小DEMO </title>
</head>
<body>
<h1>旅游网</h1>张无忌,你好。欢迎来到召唤师峡谷
<br/>
联系人:张先生
<br/>
电话:13333333 地址:西安市

<br/>
<br/>
<br/>
   你成功了

<br/>
-----水果------<br/>
    0 商品名称: 苹果 价格:5.8 <br/>
    1 商品名称: 香蕉 价格:2.5 <br/>
    2 商品名称: 橘子 价格:3.2 <br/>
共3 种
<br/>
<br/>
<br/>
名字:张飞 <br/>
分数:300
<br/>
当前日期:2018-11-13<br/>
当前时间:20:31:04<br/>
当前时间+日期:2018-11-13 20:31:04<br/>
日期格式化:2018年11月13日
<br/>
<br/>
<br/>
累计积分:120,023,412
<br/>
累计积分2:120023412

<br/>
<br/>
   aaa不存在

<br/>
bbb

</body>
</html>

  

指令介绍

      1.定义简单类型:

          <#assign name="张三">

          姓名:${name}     //姓名:张三

       2.定义对象类型

       <#assign person={"tel":"029-8888888","address":"西安"}>

        电话:${person.tel}  地址:${person.address}  //电话:029-8888888   地址:西安

      3.include 引入指令

       <#include "head.ftl">

       <#include "foot.ftl">

      4.if 判断指令

       <#if success=true>   //先要在数据模型中添加success 或者页面定义success

           成功

      <#else>

         失败

      <#if>

     5.list 循环指令

        <#list personList as person>

         ${person.name}

        </#list>

      6.内建函数   格式:变量?函数名称

         1).size函数  获取集合大小

                  ${personList?size}

         2).eval函数  将json字符串转换为json对象

            <#assign text="{ 'score':'100','name':'tom'}">

             <#assign data=text?eval/>

            分数:${data.score} 名字:${data.name}    //分数:100 名字:tom

         3).日期格式化

             注:数据模型中有Date today=new date()

             当前日期:${today?date}  //2018-11-13

             当前时间:${today?time} //20:31:04

             当前日期时间:${today?datetime} //2018-11-13 20:31:04

             日期格式化: ${today?string("yyyy年MM月dd日")}  //2018年11月13日

         4).数字转字符串

               ${sum?c}   //数字显示会三位一个分割符,不想要分割符,可转字符串

         7.空值运算处理 value??   没定义或为null存在返回true,不存在则返回false

            <#if aaa??>

              aaa变量存在

           <#else>

              aaa变量不存在

          </#if>

        8.缺失变量默认值 !   没定义或者为null 时显示默认值

          ${cup!'杯子'}

      注意事项:

           在使用比较运算符时,< ,>因为和标签混淆,会报错,使用时必须用()包起来,如<#if (a>b)>

        也可以用lt代替<;

                      gt代替>,

                      lte代替<=;

                      gte代替>=.

posted on 2018-11-14 16:31  雨后黄昏  阅读(140)  评论(0编辑  收藏  举报

导航