Java freemarker generate html page

public class FreemarkerTest (
private final Log logger = LogFactory.getLog (getClass ());
private Configuration freemarke_cfg = null;
protected Configuration getFreeMarkerCFG (String sTemplateFilePath) (
if (null == freemarke_cfg) (
freemarke_cfg = new Configuration ();
freemarke_cfg.setEncoding (Locale.CHINA, "UTF-8");
/ / Path of the template-based class loader
freemarke_cfg.setClassForTemplateLoading (this.getClass (), "/ websiteroot / freemarker");
try (
freemarke_cfg.setDirectoryForTemplateLoading (new File (sTemplateFilePath));
) Catch (Exception e) (
e.printStackTrace ();
)
)
return freemarke_cfg;
)
/ **
* Generate static files
* @ Param templatePath: template path
* @ Param templateFileName: template name
* @ Param propMap: Map storage data model
* @ Param htmlFilePath: generate HTML pages stored path
* @ Param htmlFileName: the name of generated html page
* * /
public boolean geneHtmlFile (String templatePath, String templateFileName, Map propMap, String htmlFilePath, String htmlFileName) (
try (
Template t = getFreeMarkerCFG (templatePath). GetTemplate (templateFileName);
t.setEncoding ("UTF-8");
creatDirs (htmlFilePath);
File afile = new File (htmlFilePath +"/"+ htmlFileName);
/ / Set the generated files encoded as UTF-8
/ / Server does not support UTF-8 format HTML when the HTML file using the ANSI format, that is the system default encoding
Writer out = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (afile), "UTF-8"));
t.process (propMap, out);
) Catch (TemplateException e) (
logger.error ("Error" + templateFileName, e);
return false;
) Catch (IOException e) (
logger.error ("Error" + htmlFileName, e);
return false;
)
return true;
)

public static boolean creatDirs (String path) (
File aFile = new File (path);
if (! aFile.exists ()) (
return aFile.mkdirs ();
) Else (
return true;
)
)
)

分类:Java 时间:2010-09-13 人气:185
分享到:
blog comments powered by Disqus

相关文章

  • Html static files generated using the FreeMarker 2010-03-29

    import java.io.*; import java.util.HashMap; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import freemarker.template.*; /* * Created on 2005-4-7 * */ /** * Test FreeMarker. * * @author scud

  • [Java open source tools] build static files using FreeMarker 2010-04-11

    FreeMarker is what I will not elaborate, and direct reference to China's open-source community FreeMarker description: FreeMarker is a template engine, a template generates text output based on a common tool, written using pure Java, FreeMarker is de

  • FreeMaker of static technology (5) [generate static files] 2010-08-31

    Web site is an indispensable part of the news section, and for the content of press releases are generally released after the completion of rarely changes. If you take time to click on News to read data from the server database and then displayed in

  • pre-compression nginx (gzip) static files 2010-05-15

    Compression for static files first and then the output is normal thing, but this compression is dynamic, each request will first compressed and then output to waste a lot of cpu. If the front-end plus a reverse proxy cache, it can reduce this waste.

  • nginx pre-compression (gzip) static files 2010-05-15

    Compression for static files first and then the output is normal thing, but this compression is dynamic, each request will first compressed and then output to waste a lot of cpu. If the front-end plus a reverse proxy cache, it can reduce this waste.

  • ASP Column - ASP generate static files 2010-07-16

    For Web developers, generating static files is no stranger to the concept. For Web developers, how to avoid non-stop client access when querying the database? Now there are two methods commonly used one is to use caching technology to check out the r

  • tomcat html htm static files garbled utf-8 issues 2010-09-16

    Tomcat is running in many cases, static files, including html css js is garbled, I come from the Internet search solution seems impossible. Later, careful thought for a moment, ultimately, is to load the java code when loaded by default because java load

  • To prevent the browser cache css, js static files [transfer] 2011-01-11

    Under what circumstances, to prohibit the static file cache: 1, is often possible to change the js, css. Such as a html file, test.html in the 1.0 version. It may be so <script src="common.js"> </ script> The revised version v1.1: &l

  • Display the file name contains the string: grep-lr string path 2011-05-29

    Display the file name contains the string: grep -l -r String path

  • Displays the file name containing the string: grep-lr string path 2011-05-29

    Displays the file name containing the string: grep -l -r String path

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.506 (s). 12 q(s)