This article excerpt, reproduced need to indicate the source of http://write.blog.csdn.net/postedit/8576037
Most of the time, we need to use static pages to increase the loading speed.
Let's first write a tool class for freemarker.
/*** Freemarker static file generation tool ** @ author shadow * @ email 124010356@qq.com * @ create 2012.04.28 */public class freemarkerutil {public final static string default_encoding = "UTF-8 "; private Static freemarkerutil = new freemarkerutil (); Private string encoding; private freemarkerutil () {} Public String getencoding () {If (null = encoding | "". equals (encoding) encoding = default_encoding; retur N encoding;} public void setencoding (string encoding) {This. encoding = encoding;} public configuration getconfiguration () {return configuration;} public void setconfiguration (configuration) {This. configuration = configuration;} public log getlogger () {return logger;} public static freemarkerutil getinstance () {return freemarkerutil;} public freemarkerutil getfreemarkerutil () {return freema Rkerutil;} @ suppresswarnings ("static-access") Public void setfreemarkerutil (freemarkerutil) {This. freemarkerutil = freemarkerutil;} // log object private final log logger = logfactory. getlog (getclass (); // configure the instance private configuration = NULL; private string root_path = basesupport. contextutil. getrealpath ("/"); // gets the configuration class of freemarker. freemarker supports classpath, directory, and retrieval from servletcontext. protecte D configuration getfreemarkerconfiguration () {If (null = configuration) {// initialize the freemarker instance configuration = new configuration (); // set the encoding format configuration of the instance. setdefaultencoding (getencoding (); // read the template path configuration. setservletcontextfortemplateloading (servletactioncontext. getservletcontext (), "");} return configuration;}/***** @ Param ftlpath * template file name, relative to the project path, such as "template/test. FTL "* @ Param Params * is used to process the module Attribute object ing of the Board * @ Param dirs * path of the static file to be generated, relative to the root path in the settings, for example, "template/test/Test2" * @ Param htmlname * static file name * @ return string returns the path of the static file, format: "/XX/xx.html" */@ suppresswarnings ("unchecked") Public String creat (string ftlpath, string dirpath, string htmlname, map Params) {return produce (root_path, ftlpath, dirpath, htmlname, Params);}/***** @ Param root * Custom absolute path * @ Param ftlpath * template file name, relative to the project path, such as "template/test. FTL "* @ Param Params * is used to process the attribute object ing of the template * @ Param dirpath * path of the static file to be generated, relative to the root path in the settings, for example, "template/test/Test2" * @ Param htmlname * static file name * @ return returns the path of the static file, in the format of "/XX/xx.html" */@ suppresswarnings ("unchecked ") public String creat (string root, string ftlpath, string dirpath, string htmlname, map Params) {return produce (root, ftlpath, dirpath, htmlname, Params );} @ suppresswarnings ("unchecked") Private string P Roduce (string root, string ftlpath, string dirpath, string htmlname, map Params) {try {// load template = getfreemarkerconfiguration (). gettemplate ("/" + ftlpath); // sets the template Encoding template. setencoding (getencoding (); // determines whether the directory exists. If the directory does not exist, the string dirspath = creatdirs (root, dirpath) is automatically created ); // create a file stream file = new file (root + dirspath + htmlname); // output file stream writer out = new bufferedwriter (New outputstreamwriter (ne W fileoutputstream (file), getencoding (); // set the attribute to the template. process (Params, out); out. flush (); out. close (); // return the path of the generated file: Return "/" + dirspath + htmlname;} catch (templateexception e) {logger. error (ftlpath, e); return NULL;} catch (ioexception e) {logger. error (ftlpath, e); return NULL ;}} /*** file directory check ** @ Param root * current path * @ Param dirs * file directory * @ return string returns the directory structure */private string creatdirs (strin G root, string dirpath) {stringbuffer buffer = new stringbuffer (""); If (stringutils. isnotempty (dirpath) {string [] dirs = stringutils. split (dirpath, "/| \ $"); For (string dir: dirs) {buffer. append (DIR ). append ("/");} file = NULL; If (! "". Equals (buffer. tostring () {file = new file (root + buffer. tostring (); If (! File. exists () file. mkdirs () ;}} return buffer. tostring ();}}
Create a template in the project, not necessarily at the End of. FTL.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Then, write a page for generating the call method (the parameters are modified according to their own directory conditions)
Public void test () {Map <string, Object> Params = new hashmap <string, Object> (); Params. put ("message", "this is the static html generated by the freemarker program !! "); String url = freemarkermanager. getinstance (). creat ("WEB-INF/FTL/index.html", "test/Test2", "test.html", Params); system. out. println (URL );}
You can double-click the HTML page to view the effect.
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 strict // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml">
In the future, how to access the HTML generated in the project depends on the project design.