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.
Freemarker.2.3.16.jar Download Address: http://download.csdn.net/detail/ch656409110/4494067 (this jar bag is actually inside struts2)
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.
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 >
Attached: Freemarker Tutorial
This kind of basically can calculate can be simple to do a little simple generation, but to use in practice, or poor very far, because Freemarker to the label is completely not meet our needs, this time we need to customize the label to complete our needs. Five, freemarker custom label
Freemarker Custom Label is to write their own tags, and then their own resolution, completely by themselves to control the input and output of the label, a great deal for programmers to provide a lot of space to play.
Based on steps:
The previous write tag needs to be added to the < after the #, but Freemarker to identify the custom tag needs to be followed by @, then you can define some parameters, when the program executes Template.process (Parammap, out); You will be able to parse all the Freemarker labels for the entire page.
Custom tags need to customize a class, and then implement Templatedirectivemodel, rewrite the Execute method, complete get parameters, according to the parameters do something, and so on.
Binding a custom label to a parsing class requires an instance of the parsing class to be placed in the Parammap, with the key stored in the same line as the custom label.
Note: In the custom label, if there is nothing in the label, the start tag and end tag can never be the same line, or it will error Freemarker.log.jdk14loggerfactory$jdk14logger error
I have been fooled, this is the freemarker existence of the bug.
The following is an example of 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 ">