NVelocity中调用方法 一定要通过 类.方法

 

 protected void Page_Load(object sender, EventArgs e)
    {
        IList
<UserInfo> listUsers = new List<UserInfo>();

        UserInfo objUser 
= new UserInfo();
        objUser.Name 
= "TestUser1";
        objUser.Sex 
= "M";
        objUser.City 
= "ShenZhen";
        listUsers.Add(objUser);

        objUser 
= new UserInfo();
        objUser.Name 
= "TestUser2";
        objUser.Sex 
= "F";
        objUser.City 
= "BeiJing";
        listUsers.Add(objUser);

        objUser 
= new UserInfo();
        objUser.Name 
= "TestUser3";
        objUser.Sex 
= "M";
        objUser.City 
= "USA";
        listUsers.Add(objUser);


        VelocityEngine engine 
= new VelocityEngine();
        ExtendedProperties props 
= new ExtendedProperties();
        props.AddProperty(
"file.resource.loader.path", Server.MapPath("."));
        engine.Init(props);
        Template template 
= engine.GetTemplate("HTMLPage2.htm");


        VelocityContext context 
= new VelocityContext();
        context.Put(
"name""VelocityEngine测试</br>");//修改模版页中的
        context.Put("listUsers", listUsers);

        tool tool 
= new tool();//新类
        context.Put("nono", tool);
       
        System.Text.StringBuilder builder 
= new System.Text.StringBuilder();
        builder.Append(
"#foreach($u in $listUsers)\r\n" +
            
"#beforeall\r\n" +
            
"<table border=\"0\" cellpadding=\"10\" cellspacing=\"10\">" +
            
"<tr><td>Name</td><td>Sex</td><td>City</td></tr>" +
            
"#each\r\n" +
            
"<tr>" +
            
"<td>$nono.method($u.Name)</td>" +
            
"<td>$u.Sex</td>" +
            
"<td>$u.City</td>" +
            
"</tr>" +
            
"#afterall\r\n" +
            
"</table>" +
            
"#nodata\r\n" +
            
"暂无用户资料\r\n" +
            
"#end");


        System.IO.StringWriter vltWriter 
= new System.IO.StringWriter();
        template.Merge(context, vltWriter);
        engine.Evaluate(context, vltWriter, 
null, builder.ToString());


       
// System.IO.StringWriter vltWriter = new System.IO.StringWriter();


        Response.Write(vltWriter.GetStringBuilder().ToString());

    }
posted @ 2009-05-15 17:36  awp110  阅读(428)  评论(0编辑  收藏  举报