freemarker加载模板文件的三种方法

本文介绍如何配置Freemarker模板引擎,并演示了通过设置模板路径、编码及加载方式来实现文本模板的处理过程。文章包括配置类路径、绝对路径及web路径的方法,展示了如何加载模板并进行数据填充。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.

Configuration cfg= new Configuration();       
freemarkerCfg.setClassForTemplateLoading(this.getClass(), "/");//类路径

cfg.setDirectoryForTemplateLoading(new File(System .getProperty("user.dir") + "<a href="file://\\template">\\template"));//</a>决对路径

 

2.//cfg.setServletContextForTemplateLoading(getServletContext(),"/templates");//web路径
cfg.setEncoding(Locale.getDefault(), "UTF-8");
Template template = null ;       

 

3.Locale.setDefault(Locale.ENGLISH);
try{           
        template = cfg.getTemplate("Hello.txt");
        template.setEncoding("UTF-8");
        HashMap root = newHashMap();
        map.put("user", name);
        Writer out = new StringWriter();
        Writer out = new OutputStreamWriter(System.out);
        Writer outr = new PrintWriter(new BufferedWriter(new FileWriter("d:/liyong.txt")));
        template.process(map, out);
        System.out.println(out.toString());
}catch(Exception e){
      e.printStackTrace();
}

 

 

转载于:https://www.cnblogs.com/licomeback/articles/3222522.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值