Java generation static page--freemarker instance Tutorial

Source: Internet
Author: User

Directory (?) [-] A Freemarker introduction to the two Freemarker conditions three freemarker generate the static page principle four Freemarker provides the label five Freemarker Custom label original works, allowing reprint, Reprint, please be sure to hyperlink form to indicate the original source of the article, author information and I declare. Otherwise, legal liability will be held.
Author: Eternal の_☆ Address: http://blog.csdn.net/chenghui0317/article/details/7832474 First, Freemarker introduction

Freemarker, a template engine, is a generic tool for generating static files based on templates, a development package for Java programmers, or a class library that is not intended for end users, but provides programmers with an application that can embed them in developing products.

Freemarker is written using pure Java, in order to improve the speed of page access, the need to put the page static, then Freemarker is used to generate HTML pages.

So far, Freemarker has been used more and more widely, not only for its powerful generation technology, but also for its ability to integrate well with spring.

Now start a layer to uncover its mystery.


Ii. conditions for preparation of Freemarker

Freemarker.2.3.16.jar Download Address: http://download.csdn.net/detail/ch656409110/4494067 (this jar bag is actually inside struts2)


three, Freemarker generation static page principle

Freemarker generate static pages, first need to use their own definition of the template page, this template page can be the most common HTML, can also be nested freemarker in the value expression, tags or custom tags, and so on, and then backstage read this template page, Parse the corresponding operation of the tag, and then pass the parameter substitution in the form of key-value pairs to replace the value expression in the template, and then generate a new HTML page based on the configured path to achieve the goal of static access.


Iv. tags provided by freemarker

Freemarker provides a lot of useful tags, freemarker tags are < #标签名称 > so named, ${value} represents the contents of the output variable name, as follows:

1, List: The tag is mainly for iterative server-side pass over the list collection, such as: [HTML] view plain copy print? < #list namelist as names> ${names} </#list >

Name is a loop variable that is taken when the list loop is Freemarker, which is equivalent to the following when parsing the list label:

[Java] view plain copy print?   for (String names:namelist) {System.out.println (names); 2, if: The label is mainly to do if the use of, for example:

[HTML] view plain copy print? < #if (names== "Chen Jing Revenge") > His weapon is: 15 ~ </#if > This is the Conditional Judgment label, note that the conditional equation must be enclosed in parentheses, which is equivalent to:

[Java] view plain copy print?   if (Names.equals ("Chen Jing enmity")) {System.out.println ("his weapon is: 15 ~ ~"); }
3, include: This label is used for importing files.

[HTML] view plain copy print? < #include "include.html"/>

This import tag is very useful, especially for page reuse.

In addition, you can use ${} to get the value in a static file, which is as convenient as an El expression.

Here's an example (static.html):

[HTML] view plain copy print? <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

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.