我有非常简单的速度模板:
<html>
<head>
<title>Velocity template</title>
</head>
<body>
#foreach($p in $products)
$p.name
#end
</body>
</html>
和处理它的代码:
VelocityEngine engine = new VelocityEngine();
engine.init();
Template t = engine.getTemplate("./src/com/irbis/dms/velocity/template.html");
VelocityContext ctx = new VelocityContext();
Product p1 = new Product("fridge");
Product p2 = new Product("sofa");
Product p3 = new Product("table");
Product p4 = new Product("chair");
List<Product> products = new ArrayList<Product>();
products.add(p1);
products.add(p2);
products.add(p3);
products.add(p4);
ctx.put("products", products);
...
class Product implements Serializable {
private String name;
public Product() {
}
public Product(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
但执行后我有以下内容:
<html>
<head>
<title>Velocity template</title>
</head>
<body>
$p.name
$p.name
$p.name
$p.name
</body>
</html>
如果我将字符串、整数等放入上下文中,它工作正常。错误在哪里?我使用速度 1.5。