将对象作为 "root" 放入 VelocityContext
Put object as "root" to the VelocityContext
我想访问模板中某个模型 class 的所有属性。如果我这样做
VelocityContext context = new VelocityContext();
context.put("model", new MyModel());
和MyModel
就像
public class MyModel {
private String propertyA;
private String propertyB;
public String getPropertyA() { return propertyA; }
public String getPropertyB() { return propertyB; }
}
然后我需要在每次访问它的属性时指定模型的别名。
This is my template with properties like $model.propertyA and $model.propertyB.
我想实现的是模板变量不需要指定 model.
作为某个上下文成员的前缀,如下所示:
This is my template with properties like $propertyA and $propertyB.
每个变量都应被视为 MyModel
类型的给定 "root" 对象的 属性。这可能吗?如果可能,怎么做?
Velocity 无法做到这一点 - 无法指定 'default' 对象。 属性 和方法引用只有在其对象名称前面才能被正确解析。
我想访问模板中某个模型 class 的所有属性。如果我这样做
VelocityContext context = new VelocityContext();
context.put("model", new MyModel());
和MyModel
就像
public class MyModel {
private String propertyA;
private String propertyB;
public String getPropertyA() { return propertyA; }
public String getPropertyB() { return propertyB; }
}
然后我需要在每次访问它的属性时指定模型的别名。
This is my template with properties like $model.propertyA and $model.propertyB.
我想实现的是模板变量不需要指定 model.
作为某个上下文成员的前缀,如下所示:
This is my template with properties like $propertyA and $propertyB.
每个变量都应被视为 MyModel
类型的给定 "root" 对象的 属性。这可能吗?如果可能,怎么做?
Velocity 无法做到这一点 - 无法指定 'default' 对象。 属性 和方法引用只有在其对象名称前面才能被正确解析。