## Freemarker template engine Share

Static web technology -Freemarker


 The reason: In our development process, in general, to solve the problem will be a substantial part of static pages, news sites such as those of the static news, another example of our major electricity supplier items, each item's detail page, are some static resources, If every user access to the database to be accessed, then it will cause high concurrent phenomenon, but also a waste of resources.

Technology : static web technology and caching technology in common is to reduce the pressure to access the database, but depending on the specific application scenarios, the cache is more suitable for small-scale data, and static pages are more suitable for large-scale and relatively infrequent changes The data. Also static web page is also beneficial to SEO .

  In addition, we show the page if the pure static form, you can use Nginx such a high-performance web server to deploy. Nginx can carry 5 million for concurrency, and Tomcat only a few hundred.

## First, that we come to know what time Freemarker right?


    FreeMarker is a Java written language template engine, which is based on a template to generate text output. FreeMarker and Web -independent container, namely in Web runtime, it does not know Servlet or HTTP . It not only can be used to achieve technical presentation layer, but also can be used to generate XML , JSP , or Java and so on.

## Second, we come to a small engineering experience under:


 Project Dependencies:

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

 The previous time we might also written to or written request for leave many people would like to write, you will find something that is not a template it?

** Dear , Hello!
    ************************************************** ********************** 
                                                signature: 
                                                date:

So we have to create a today:

<HTML> 
<head> 
    <Meta charset = " UTF-8 " > 
    <title> Getting Freemarker small DEMO </ title> 
</ head> 
<body> 
<# - I'm just a comment, I do not have any output - -> 
$ {name}, hello. Message {} $
 </ body> 
</ HTML>

Let's create a class:

cn.liurui.core.demo Package; 

Import freemarker.template.Configuration; 
Import freemarker.template.Template; 

Import java.io.File; 
Import java.io.FileWriter; 
Import java.util.Date; 
Import the java.util.HashMap ; 
Import a java.util.Map; 

/ * * 
 * @author liurui 
 * @date DATE $ {12:04} 
 * / 
public  class Freemarker_Demo {
     public  static  void main (String [] args) throws Exception {
         // prepare data model is json data, the data type may be javabean 
        the Map Datamap = new new the HashMap ();
     // configuration FreeMarker
         //Configuration setting configuration 
        the Configuration configuration = new new the Configuration (Configuration.VERSION_2_3_23);
         // set the template path 
        configuration.setDirectoryForTemplateLoading ( new new File ( " D: / the Users / Administrator / 
      shop_pinyougou_demo_parent / freemarker_demo / the src / main / Resources
" )); // setting template content encoding configuration.setDefaultEncoding ( " UTF-. 8 " ); // set the template object template template = configuration.getTemplate ( " index.ftl " ); // set the template file FileWriter writer = new FileWriter("D:/1.html"); //合成输出 template.process(dataMap,writer); //关闭 writer.flush(); writer.close(); } }
    // 5. Create a data model 
        the Map the Map = new new HashMap (); 
        map.put ( " name " , " Joe Smith " ); 
        map.put ( " the Message " , " Welcome to the magical Freemarker world! " );

Then we start the main method: We will see output Joe Smith on the page, Freemarker Welcome to the magical world!

如果这个Freemarker模板仅仅只能做到这么简单的,那我们也不会用它是吧。。。。

下面我们来看看Ftl指令:


 <#assign linkman="赵四">

联系人:${linkman}//联系人:赵四
<#assign info={"mobile":"110",'address':'黄河西路'} >
电话:${info.mobile}  地址:${info.address}//电话:110  地址:黄河西路

##一,我们一般网页前端写的页面,如果几个页面头部相同,会单独写一个头部,然后通过inculde命令去导入,那么我们模板也会有这个指令:

<#include "head.ftl">

##二,if指令

在我们的类中我们会添加一个:

dataMap.put("success",false);//当为true时,为ok,当为false时,为no
<#if success=true>
  ok
<#else>  
  no
</#if>

##三,list指令

        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);

在我们的模板中添加;

----商品价格表----<br>
<#list goodsList as goods>
  ${goods_index+1} 商品名称: ${goods.name} 价格:${goods.price}<br>
</#list>
打印结果:
1 商品名称:苹果 价格:5.8
2 商品名称:香蕉 价格:2.5
3 商品名称:橘子 价格:3.2

##四,我们需要将json字符串转成对象:

    <#assign text="{'bank':'工商银行','account':'10101920201920212'}" />
    <#assign data=text?eval />
    开户行:${data.bank}  账号:${data.account}

##五,日期格式:

  Map dataMap = new HashMap();
        dataMap.put("today",new Date());
        /**
         * 当前日期:2019-9-30
         当前时间:16:38:50
         当前日期加时间;2019-9-30 16:38:50
         日期格式化:2019年09月30日 16时38分50秒
         */
当前日期:${today?date}<br>
当前时间:${today?time}<br>
当前日期加时间;${today?datetime}<br>
日期格式化:${today?string("yyyy年MM月dd日 HH时mm分ss秒")}

##六,判断变量是否存在?

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

Guess you like

Origin www.cnblogs.com/liurui-bk517/p/11613846.html