FreeMarker preface & amp; chapter 2 getting started, freemarker Preface

Source: Internet
Author: User

FreeMarker note preface & Chapter 2 getting started, freemarker note Preface
Introduction

FreeMarker is a template engine. It is a template-based general tool used to generate output text (any text in HTML format is used to automatically generate source code. It is an development kit or a class library provided for Java programmers. It is not intended for end users, but an application provided for programmers to embed their development products. Features, basic features, summary, and keyword recommendations

FreeMarker is a template engine. It is a template-based general tool used to generate output text (any text in HTML format is used to automatically generate source code. It is an development kit or a class library provided for Java programmers. It is not intended for end users, but an application provided for programmers to embed their development products.

FreeMarker is designed to generate HTML web pages, especially through Servlet applications that implement the Model View Controller (Model-View-Controller) mode. The idea of using dynamic web pages in MVC mode allows you to separate front-end designers (write HTML) from programmers. Each person performs his/her own duties and gives full play to his/her expertise. Web designers can rewrite the page display effect without being influenced by the programmer's code compilation, because the logic of the application (Java program) and the page design (FreeMarker template) have been separated. Page Template code is not affected by complex program code. This idea of separation is even useful for projects where programmers and page designers are the same person, because separation keeps code concise and easy to maintain.

FreeMarker is not a Web application framework. It is an applicable component in the Web application framework. Chapter 2 getting started 1st templates + Data Models = Output 1.2 data models overview 1.3 templates overview 1.4.1 Introduction

FTL tags tag: Language tag of the FreeMarker template. Generally#The user-defined FTL label@Replace#.

Comments: The annotation of FreeMarker is similar to that of HTML, but it uses<#--And-->.

Directives command: Refers to the FTL tag. 1.4.2 command example 1.4.2.1 if command

Suppose you just want to say hello to your boss Big Joe (not others), you can do this:

Use the <# else> label:

<#if animals.python.price < animals.elephant.price>    Pythons are cheaper than elephants today.<#else>    Pythons are not cheaper than elephants today.</#if>

If the variable itself is a Boolean value, it can be directly used as the if condition;

<#if animals.python.protected>    Warniing! Pythons are protected animals!</#if>
Instance

/FreeMarker-hello-web/src/main/java/org/yejq/fre/model/Animal. java

public class Animal {    private String name;    private double price;    private boolean protect;    。。。}

/FreeMarker-hello-web/src/main/java/org/yejq/fre/service/Exercises. java

    public void testIf(Model model){        model.addAttribute("user", "Big Joe");                Map<String, Animal> animals = new HashMap<String, Animal>();        animals.put("python", new Animal("python", 300, true));        animals.put("elephant", new Animal("elephant", 400, false));        model.addAttribute("animals", animals);    }

/FreeMarker-hello-web/src/main/webapp/WEB-INF/ftl/2/if. ftl

<! Doctype html> 

Test: http: // localhost/test/2/if/testIf1.4.2.2 list Command

When you need to traverse the content of a set, the list command is very useful.

<#list animals as being>    <tr><td>${being.name}<td>${being.price} Euros</#list>
Instance

/FreeMarker-hello-web/src/main/java/org/yejq/fre/service/Exercises. java

    public void testList(Model model){        List<Animal> animals = new ArrayList<Animal>();        animals.add(new Animal("python", 300, true));        animals.add(new Animal("elephant", 400, false));        model.addAttribute("animals", animals);    }

/FreeMarker-hello-web/src/main/webapp/WEB-INF/ftl/2/list. ftl

<H3> list Command 

Test:

Http: // localhost/test/2/list/testList1.4.2.3 include command

Insert the content of other files in the current template.

Copyright_footer.html:

When copyright is required

<#include "/copyright_footer.html">
Instance

/FreeMarker-hello-web/src/main/webapp/WEB-INF/ftl/2/copyright.html

<Hr> <I> Copyright (c) 2000 <a href = "http://www.xqsoso.com"> Acmee Inc </a>, <br> All Rights Reserved. Chinese test </I>

/FreeMarker-hello-web/src/main/webapp/WEB-INF/ftl/2/include. ftl

<H3> include command 

Test: commands used in combination with http: // localhost/test/2/include/null1.4.2.4

Commands can be nested for use; 1.4.2.5 handling non-existent Variables

By following a variable name!And default value.

Yes ?? Ask freemarker whether a variable exists and merge it with the if command. if the user variable does not exist, the entire greeting code segment will be ignored;

<#if user??>

For variables with multi-level access, such as animals. python. price, write the code: animals. python. price! 0, only when animals. python exists and the price of the last sub-variable may not exist (in this case, we assume the price is 0 ). If animals or python does not exist, the template processing will stop with the "undefined variable" error. To prevent this situation, you can write the code in this way.(animals.python.price)!0. In this case, when animals or python does not exist, the expression returns 0. For ?? Animals. python. price ?? Comparison (animals. python. price )?? See; instance

/FreeMarker-hello-web/src/main/webapp/WEB-INF/ftl/2/null. ftl

<H3> process nonexistent variables 

Test: http: // localhost/test/2/null

References
Freemarker details

FreeMarker is a Java template engine that generates text output based on the template. FreeMarker has nothing to do with the Web Container, that is, when the Web is running, it does not know Servlet or HTTP. It can not only be used as the Implementation Technology of the presentation layer, but also be used to generate XML, JSP or Java.
FreeMarker allows Java servlet to maintain the separation of graphic design from application logic, which is achieved by sealing HTML in the template. The template uses the data provided by servlet to dynamically generate HTML. The template language is powerful and intuitive. The Compiler speed is fast, and the output speed is close to that of Static HTML pages. Although FreeMarker has some programming capabilities, the data to be displayed is usually prepared by the Java program, and the page is generated by FreeMarker to display the prepared data through the template.
Features
General
Can generate various texts: HTML, XML, RTF, Java source code, and so on.
Easy to embed into the product: lightweight; no Servlet environment is required.
Plug-in template Loader: you can load templates from any source, such as local files and databases.
You can generate text as needed: Save it to a local file, send it as Email, and send it back to a Web browser from a Web application. [4]
Powerful template Language
All common commands: include, if/elseif/else, and loop structure.
Create and change variables in the template.
You can use a complex expression to specify a value almost anywhere.
A named macro that can have location parameters and nested content.
Namespace helps you create and maintain reusable macro libraries, or split a large project into modules without worrying about name conflicts.
Output conversion block: HTML Escape, compression, and syntax highlighting are converted when a nested template segment generates an output. You can define your own conversion. [4]
General Data Model
FreeMarker does not directly reflect to Java objects. Java objects are encapsulated by plug-in objects and displayed in the template as variables.
Abstract (Interface) can be used to represent objects (JavaBean, XML documents, SQL query result sets, etc.), telling template developers to use them. To avoid technical details.
Prepare for Web
Built-in structure for processing typical Web-related tasks (such as HTML escaping) in the template language.
Can be integrated into the Model2 Web application framework as a substitute for JSP.
Supports JSP tag libraries.
Design for MVC mode: Separate visual design and application logic; Separate page designers and programmers. [4]
Intelligent internationalization and Localization
Intelligent Character Set (UNICODE is used internally ).
The digital format is sensitive to localization.
Date and Time formats are sensitive to localization.
Non-US character sets can be used as Identifiers (such as variable names ).
The same template in multiple languages. [4]
Powerful XML processing capabilities
The <# recurse> and <# visit> commands (Version 2.3) are used to recursively traverse the XML tree.
Clearly and intuitively access the XML Object Model in the template. [2] [4]

What is FreeMarker?

It is a Java package and class library for Java programmers. It is not an end-user application, but allows programmers to embed it into their products. FreeMarker is designed to generate HTML Web pages, especially MVC (Model View Controller)-based applications. The idea of using the MVC pattern as a dynamic web page is to separate page designers (HTML designers) and programmers .. Everyone makes what they are good. Designers can change the web page without modifying the programmer's code, because the application logic (Java program) and page design (FreeMarker template) are separated. Templates will not be damaged by complex and complex program frameworks. This separation is useful even when the programmer of a project and the creator of the HIMTL page are the same person, because it helps to keep the application clear and easy to maintain. Although FreeMarker has some programming capabilities, it is not a mature programming language like PHP. Different from Java, Java programs prepare data for display (such as SQL queries), while Freemarker only generates a text page, which displays the preparation data for the template. FreeMarker is not a Web application framework, but is more suitable for serving as a component in the Web application framework. However, FreeMarker engine itself does not care about HTTP or servlets, but only generates text. In a non-Web application environment, it can also demonstrate its perfect use. However, it is worth noting that we provide a convenient solution for using FreeMarker. As a view component of the Model2 framework (such as Struts), you can also use JSP labels in the template. FreeMarker is free of charge, and BSD-style licenseServlet runs on Java-enabled Web Server. Java Servlet can dynamically expand Server capabilities and provide Web Services in the request-response mode. The earliest Java Web Server that supports Servlet technology is JavaSoft. Later, some other Java-based Web servers began to support Standard

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.