FreeMarker learning and freemarker Learning

Source: Internet
Author: User

FreeMarker learning and freemarker Learning

1. FreeMarker

FreeMarker is a template engine. It is a common tool based on templates and data to be changed and used to generate output text (HTML webpage, email, configuration file, source code, etc. It is not intended for end users, but a Java class library. It is a component that programmers can embed into their products. (Baidu encyclopedia)

FreeMarker is not a Web application framework, but is suitable as a component of the Web application framework.

FreeMarker has nothing to do with containers because it does not know HTTP or Servlet. FreeMarker can also be applied to non-Web application environments.

Advantages:

1.FreeMarkerAdvantage 1To completely separate the presentation layer and business logic.
During JSP development, a large amount of code with business logic is found in the page, which makes the page content messy and becomes very difficult in the later process of modification and maintenance.
FreeMarker does not support Java Script code at all.
FreeMarker's principle is: Template + Data Model = output. The template is only responsible for the presentation of data on the page and does not involve any logic code. All logics are processed by the data model. The final output is created after the template and data model are merged.

2.FreeMarkerAdvantage 2To improve development efficiency.
JSP pages are used to display data, that is, the so-called presentation layer. As we all know, JSP needs to be converted into a Servlet class during the first execution. during the development stage, the function is adjusted in a timely manner and JSP needs to be modified frequently. Compilation and conversion are required for each modification, let's imagine how much time we waste in program compilation in a day. Compared with JSP, FreeMarker template technology does not have compilation and conversion problems, so the above problems do not exist. In addition, during the development process, you do not have to wait for the interface design and development personnel to complete the page prototype before you start to develop the program.

Disadvantages:

1.FreeMarker disadvantage 1By using FreeMarker template technology, expired data may be displayed after the template is modified. For example, after a static HTML page is generated, if the template is changed but the HTML page generated by the template is not updated in time, the user will see expired data.
2.FreeMarker's disadvantage 2FreeMarker does not support cluster applications. For convenience of compilation, all serialized items are put into the Session, such as Session and request, which is indeed convenient in the development process. However, if the application is put into the cluster, an error occurs.

Ii. Data Format

The data format of freeMark is a tree structure, and the data key encapsulated by Map must be string. Other data types cannot be operated.

Iii. Use

 

Public static void main (String [] args) throws IOException, TemplateException {// create Freemarker Configuration instance Configuration cfg = new Configuration (); cfg. setDirectoryForTemplateLoading (new File ("templates"); // create a data model Map <String, Object> root = new HashMap <String, Object> (); root. put ("name", "zhangsan"); // string type root. put ("random", new Random (). nextInt (100); List <Map <String, Object> listMap = new ArrayList <Map <String, Object> (); Map <String, object> map = new HashMap <String, Object> (); map. put ("name", "A1"); map. put ("name", "A2"); listMap. add (map); root. put ("listMap", listMap); // load the Template file template = cfg. getTemplate (". ftl "); // display the generated data Writer writer = new OutputStreamWriter (System. out); // Writer writer = new OutputStreamWriter (new FileOutputStream ("html/index.html"), "UTF-8"); template. process (root, writer); writer. flush ();}

 

A. ftl

Hello $ {name} Welcome to FreeMarker <# -- if statement --> # if statement <# if name = "Zhang San"> Zhang San </# if> <# -- if else --> # gt greater than or equal to gte <# if random gt 60 >$ {random }: pass <# else >$ {random}: fail </# if> <# -- if else statement test: --> <# if random gte 90 >$ {random}: Excellent! <# Elseif random gte 80 >$ {random}: Good! <# Else >$ {random}: normal! </# If> <# -- traverse List --> <# list listMap as map> <B >$ {map. name} </B> <br/> </# list> <# -- include --> <# include "index. ftl "/> <# -- custom command macro command --> <# macro m1> <# -- Definition command m1 --> <B> aaabbbccc </B> <B> dddeeefff </B> </# macro> <@ m1/> <# -- call the preceding macro command --> <# -- Define the macro command with parameters: --> <# macro m2 a B c >$ {a} -- $ {B} -- $ {c} </# macro> <@ m2 a = "" B =" old Zhang "c =" Old Ma "/> <# -- nested command: --> <# macro border> <table border = 4 cellspacing = 0 cellpadding = 4> <Tr> <td> <# nested> </td> </tr> </table> </# macro> <@ border> table content! Method 2 </@ border> <# -- namespace --> <# import "B. ftl "as bb/> <@ bb. copyright date = "2010-2011"/>$ {bb. mail} <# assign mail = "my@163.com"/>$ {mail} <# -- reference is --> <# assign mail = "my@126.com" in bb/>$ {bb. mail} <# -- reference -->View Code

B. ftl

<# Macro copyright date> <p> Copyright (C) $ {date} Beijing Shang. </p> </# macro> <# assign mail = "bjsxt@163.com">View Code

Index. ftl

 

<# Macro copyright date> <p> Copyright (C) $ {date} </p> </# macro> <# assign mail = "bjsxt@163.com">View Code

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.