freemarker模板技术

示例:

import  freemarker.template.Configuration;
import  freemarker.template.SimpleHash;
import  freemarker.template.Template;
import  freemarker.template.TemplateException;

public   class  Test  {
    
public static Log log = LogFactory.getLog(Test.class);

    
public static void main(String[] args) {
try {
            
/*
            定义中的文件内容 test.ftl
            ${title}
            ${author}
            ${content}
            
*/

            Configuration cfg 
= new Configuration();
            String tempDir
="d:/test";//定义模板存放路径
            
//根据所在地方定义路径String tempDir= URLDecoder.decode(this.getClass().getClassLoader().getResource("../").getPath(), "UTF-8");
            String tempName="test.ftl";//定义模板名
            String outputDir="d:/test";//定义模板输出路径
            String outputName="d:/test.txt";//定义模板输出文件名
            cfg.setDirectoryForTemplateLoading(new File(tempDir));
            Template temp 
= cfg.getTemplate(tempName, Locale.US, "UTF-8");
            SimpleHash map 
= new SimpleHash();
            map.put(
"title""hello");
            map.put(
"author""beauty9235");
            map.put(
"content""hello,world!");
            
//定义将结果打印出来
            StringWriter out = new StringWriter();
            temp.process(map, out);   
            log.debug(out.toString());
            
//定义生成文件
            File output = new File(outputDir, outputName);
            Writer writer 
= new FileWriter(output);
            
try {
                temp.process(map, writer);
            }
 catch (TemplateException e) {
                e.printStackTrace();
            }

            
        }
 catch (TemplateException t) {
           log.debug(t.getMessage());
        }
 catch (IOException i) {
            log.debug(i.getMessage());
        }

        }

}
public void geneHtmlFile(String templateFilePath, String htmlFilePath, SimpleHash simpleHash) {
         try {
             Configuration cfg = new Configuration();
             cfg.setDefaultEncoding("UTF-8");
             cfg.setOutputEncoding("UTF-8");
             cfg.setLocale(Locale.US);
             cfg.setDirectoryForTemplateLoading(new File(StringUtils.substringBeforeLast(templateFilePath, "//")));
             Template temp = cfg.getTemplate(StringUtils.substringAfterLast(templateFilePath, "//"));
             Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(htmlFilePath)), "UTF-8"));
             temp.process(simpleHash,out);
         } catch (IOException e) {
             log.debug(e);
         } catch (TemplateException e) {
             log.debug(e);
         }
     }
       public void geneHtmlFile(String templateFilePath, String htmlFilePath, Map propMap) {
         try {
             Configuration cfg = new Configuration();
             cfg.setDefaultEncoding("UTF-8");
             cfg.setOutputEncoding("UTF-8");
             cfg.setLocale(Locale.US);
             cfg.setDirectoryForTemplateLoading(new File(StringUtils.substringBeforeLast(templateFilePath, "//")));
             Template temp = cfg.getTemplate(StringUtils.substringAfterLast(templateFilePath, "//"));
             Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(htmlFilePath)), "UTF-8"));
             temp.process(propMap,out);
         } catch (IOException e) {
             log.debug(e);
         } catch (TemplateException e) {
             log.debug(e);
         }
     }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值