我想访问模板中某个模型类的所有属性。如果我做

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的给定“根”对象的属性。这可能吗?如果可以,怎么办?

最佳答案

对于Velocity,这是不可能的-无法指定“默认”对象。属性和方法引用只有在其对象名称开头的情况下才能正确解析。

08-05 16:07