freemarker

来源:互联网 发布:java string 去掉空格 编辑:程序博客网 时间:2024/06/04 04:46

FreeMarker(一)

freemark 是一款模板引擎,将数据静态化成html当然还有其他类型的文件
maven 依赖

`<dependency>            <groupId>org.freemarker</groupId>            <artifactId>freemarker</artifactId>            <version>2.3.20</version></dependency>`

模板生成方法

`protected void process(File baseDir, String ftlName, String htmlPath, Map<String, Object> rootMap)            throws Exception {        Configuration configuration = new Configuration();        configuration.setEncoding(Locale.getDefault(), "UTF-8");        TemplateLoader templateLoader = new FileTemplateLoader(baseDir);        configuration.setTemplateLoader(templateLoader);        Template template = configuration.getTemplate(ftlName);        template.setEncoding("UTF-8");        Writer out = new OutputStreamWriter(new FileOutputStream(htmlPath), "UTF-8");        processNav(rootMap);        rootMap.put("basePath", basePath);        template.process(rootMap, out);    }

baseDir: 模板文件所在路径文件 ftlName:模板名字 rootMap:需要设置到process到前面的数据
ps: 如果需要<#include />其他模板 ./ 和 /是相对于baseDir的

0 0