内容目录
一. 没有 getter & setter 的 object[] (失败)
二. 有 getter & setter 的 object[]
三. 有 getter & setter 的 ArrayList
测试模板 ObjectTemplate.vm
一. 没有 getter & setter 属性的 object[] ,失败
1. 对象声明:
2. NVelocityTemplateEngine 测试用例:
3. 输出:
$product.Name $product.Name $product.Name
二. 有 getter & setter 的 object[]
1. 对象声明
2. NVelocityTemplateEngine 测试用例: 同上例
3. 输出:
aaa bbb ccc
三. 有 getter & setter 的对象的 ArrayList
1. 对象声明:同上例
2. NVelocityTemplateEngine 测试用例
3. 输出
aaa bbb ccc
一. 没有 getter & setter 的 object[] (失败)
二. 有 getter & setter 的 object[]
三. 有 getter & setter 的 ArrayList
测试模板 ObjectTemplate.vm
#foreach($product in $products)
$product.Name
#end
$product.Name
#end
一. 没有 getter & setter 属性的 object[] ,失败
1. 对象声明:
public class Product
{
public string Name;
}
{
public string Name;
}
2. NVelocityTemplateEngine 测试用例:
private void ProcessObjectTemplate()
{
LabelFile.Visible = true;
LabelMergedFile.Visible = true;
string templateDir = HttpContext.Current.Server.MapPath("Templates");
string templateName = "ObjectTemplate.vm";
INVelocityEngine fileEngine =
NVelocityEngineFactory.CreateNVelocityFileEngine(templateDir, true);
IDictionary context = new Hashtable();
Product[] products = new Product[3];
Product p1 = new Product();
p1.Name = "aaa";
Product p2 = new Product();
p2.Name = "bbb";
Product p3 = new Product();
p3.Name = "ccc";
products[0] = p1;
products[1] = p2;
products[2] = p3;
context.Add("products", products);
LabelMergedFile.Text = fileEngine.Process(context, templateName);
}
{
LabelFile.Visible = true;
LabelMergedFile.Visible = true;
string templateDir = HttpContext.Current.Server.MapPath("Templates");
string templateName = "ObjectTemplate.vm";
INVelocityEngine fileEngine =
NVelocityEngineFactory.CreateNVelocityFileEngine(templateDir, true);
IDictionary context = new Hashtable();
Product[] products = new Product[3];
Product p1 = new Product();
p1.Name = "aaa";
Product p2 = new Product();
p2.Name = "bbb";
Product p3 = new Product();
p3.Name = "ccc";
products[0] = p1;
products[1] = p2;
products[2] = p3;
context.Add("products", products);
LabelMergedFile.Text = fileEngine.Process(context, templateName);
}
3. 输出:
$product.Name $product.Name $product.Name
二. 有 getter & setter 的 object[]
1. 对象声明
public class Product
{
private string name;
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
}
{
private string name;
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
}
2. NVelocityTemplateEngine 测试用例: 同上例
3. 输出:
aaa bbb ccc
三. 有 getter & setter 的对象的 ArrayList
1. 对象声明:同上例
2. NVelocityTemplateEngine 测试用例
private void ProcessObjectTemplate()
{
LabelFile.Visible = true;
LabelMergedFile.Visible = true;
string templateDir = HttpContext.Current.Server.MapPath("Templates");
string templateName = "ObjectTemplate.vm";
INVelocityEngine fileEngine =
NVelocityEngineFactory.CreateNVelocityFileEngine(templateDir, true);
IDictionary context = new Hashtable();
Product p1 = new Product();
p1.Name = "aaa";
Product p2 = new Product();
p2.Name = "bbb";
Product p3 = new Product();
p3.Name = "ccc";
ArrayList products = new ArrayList();
products.Add(p1);
products.Add(p2);
products.Add(p3);
context.Add("products", products);
LabelMergedFile.Text = fileEngine.Process(context, templateName);
}
{
LabelFile.Visible = true;
LabelMergedFile.Visible = true;
string templateDir = HttpContext.Current.Server.MapPath("Templates");
string templateName = "ObjectTemplate.vm";
INVelocityEngine fileEngine =
NVelocityEngineFactory.CreateNVelocityFileEngine(templateDir, true);
IDictionary context = new Hashtable();
Product p1 = new Product();
p1.Name = "aaa";
Product p2 = new Product();
p2.Name = "bbb";
Product p3 = new Product();
p3.Name = "ccc";
ArrayList products = new ArrayList();
products.Add(p1);
products.Add(p2);
products.Add(p3);
context.Add("products", products);
LabelMergedFile.Text = fileEngine.Process(context, templateName);
}
3. 输出
aaa bbb ccc