Programming using Freemarker static Web page __java

Source: Internet
Author: User

1, introduce-freemarker is what

Template engine: A generic tool for generating output text based on a template

Java-based development packages and class libraries

2, introduce what-freemarker can do

The view layer component in the MVC framework

HTML page static

Code generation Tools

CMS template engine

Page column Dynamic customization

3, Introduction-Why to use Freemarker

Separation of Program logic (Java Program) and page design (freemarker template)

Clear stratification and good division of the Division

Good integration of mainstream web frameworks (STRUTS2,SPRINGMVC)

Easy to learn, powerful

Free Open Source

4, Freemarker advantages

Freemarker is not dependent on a servlet, network, or web environment

Freemarker was designed for MVC in the first, and it just focused on showing

You can load the template from anywhere, from the class path, from the database medium

Easy to define ad hoc macros and functions

5, the above simple introduction to Freemarker, the following is mainly the use of Freemarker Internship Web site static function.

Through the above introduction to know Freemarker is a template-based, used to generate output text of the common tools, so we have to customize the template to meet their own business, and then generate our HTML page

Freemarker is loading the template by freemarker.template.Configuration this object (it also handles the work of creating and caching the pre-resolution template), and then we get the template you want through the GetTemplate method, one thing to remember Freemarke R.template.configuration must guarantee a unique instance throughout your application.

5.1, in the configuration can use the following methods to facilitate the establishment of three kinds of template loading

[Java]View Plain copy print? void Setdirectoryfortemplateloading (File dir);
[Java]View Plain copy print? void Setclassfortemplateloading (Class cl, String prefix);
[Java]View Plain copy print?   void Setservletcontextfortemplateloading (Object servletcontext, String path); The first of these methods sets up a clear directory on the disk's filesystem, which determines where to load the template. Don't say maybe, the File parameter is definitely a directory that exists. Otherwise, an exception will be thrown.
The second invocation method uses a parameter with a class type and a prefix. This allows you to specify when to load the template with the same mechanism, but to load the class with Java ClassLoader. This means that the biography
The class parameter entered is used to invoke the Class.getResource () method to locate the template. The parameter prefix is prefixed to the name of the template. In a real-world environment, the class-loading mechanism is the preferred method of loading a template, because this mechanism of loading files from the classpath is generally more secure and simpler than loading a file from a specific directory location. In a final application, it's also nice to have all the code packaged with a. jar file, so that users can execute the. jar file that contains all the resources directly.
The third invocation method requires the context of the Web application and a base path as a parameter, which is the relative path of the Web application root path (the parent directory of the Web-inf directory). The loader will then open from the Web application directory
Start loading the template. Although the Load method works on a. War file that is not packaged, because it uses the Servletcontext.getresource () method to access the template, notice that we refer to the "directory" here. If you omit the second argument (or use ""), you can mix the static files (. html,.jpg, and so on) and the. ftl file, except that the. ftl file can be sent to the client for execution. Of course, a servlet must be configured in Web-inf/web.xml to handle a user request with a URI format of *.FTL, otherwise the client cannot get the template, so you will see the secret hints from the Web server. You cannot use empty paths in your site, which becomes a problem, you should store the template files somewhere in the Web-inf directory so that the template source files will not accidentally
void Setdirectoryfortemplateloading (File dir);
void Setclassfortemplateloading (Class cl, String prefix);
void Setservletcontextfortemplateloading (Object
ServletContext, String Path);
To be executed, this mechanism is a great way to load a template for a servlet application, and the template can be updated automatically without restarting the Web application, but for the class-loading mechanism, it won't work.

5.2. Loading templates from multiple locations

[Java] view plain copy print? Import freemarker.cache.*;   The template loader is under this package ...   Filetemplateloader FTL1 = new Filetemplateloader (New File ("/tmp/templates"));   Filetemplateloader FTL2 = new Filetemplateloader (New File ("/usr/data/templates"));   Classtemplateloader ctl = new Classtemplateloader (GetClass (), "");   templateloader[] Loaders = new templateloader[] {ftl1, ftl2,ctl};   Multitemplateloader MTL = new Multitemplateloader (loaders);   Cfg.settemplateloader (MTL); Now, Freemarker will try to load the template from the/tmp/templates directory, and if the requested template is not found in this directory, it will continue to try to load from the/usr/data/templates directory, if the requested template is still not found, It then uses the class loader to load the template.

5.3. Encapsulation Freemarker for creating templates and loading templates

[Java]   View plain copy print package com.ajun.template.utils;       import java.io.ioexception;   import java.io.writer;   import  java.util.locale;   import java.util.map;      import  javax.servlet.servletcontext;      import freemarker.template.configuration;    Import

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.