使用VELOCITY生成文件

        VelocityEngine velocity = new VelocityEngine();
        Properties properties = new Properties();
         //设置属性

    。。。。

        velocity.init(properties);    

 

        // ファイルWriter
        Writer writer = null;
        try {
            String charSet = "UTF-8";
            Template template = velocity .getTemplate(”PACKAGE+文件名”);

            Context context = new VelocityContext();
            context.put("DAT", "");
            writer=new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream("PACKAGE+新文件名"),charSet)));

            template.merge(context, writer);
            writer.flush();
            writer.close();
        } catch (Exception e) {

        }

 

 

参照:

 

  a) 创建Velocity 引擎(VelocityEngine)并设置属性.


  b) VelocityContext 高低文对象创建于设置.


  c) 应用VelocityEngine(Velocity 引擎)创建模板(Template).


  d) 归并模板和高低文对象、输出.


 
<% @ WebHandler Language = " C# " Class = " ShowHTML " %>

using System;
using System.Web;

// NVelocity 引用
using NVelocity;
using NVelocity.App;
using NVelocity.Runtime;

public class ShowHTML : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
// 1.创建Velocity 引擎(VelocityEngine)并设置属性
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.AddProperty(RuntimeConstants.RESOURCE_LOADER,
" file " ); // Velocity加载类型
velocityEngine.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, // Velocity加载文件路径
context.Server.MapPath( " ~/Template/ " ));
velocityEngine.AddProperty(RuntimeConstants.INPUT_ENCODING,
" gb2312 " ); // 输入编码格局设置
velocityEngine.AddProperty(RuntimeConstants.OUTPUT_ENCODING, " gb2312 " ); // 输出编码格局设置
velocityEngine.Init();

// 2.Velocity 高低文对象设置
VelocityContext vc = new VelocityContext();
// 页面参数设值
vc.Put( " Name " " MT! " );
System.Collections.Generic.List
< String > list = new System.Collections.Generic.List < string > ();
for int i = 1 ; i < 11 ; i ++
{
list.Add(
" My Name Is : " + i);
}
vc.Put(
" list " , list);

// 3.创建模板(Template)
Template template = velocityEngine.GetTemplate( " default.html " );

// 4.归并模板和高低文对象、输出
template.Merge(vc, HttpContext.Current.Response.Output);
HttpContext.Current.Response.End();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值