我想访问模板中某个模型类的所有属性。如果我这样做
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 来说是不可能的 - 无法指定“默认”对象。仅当前面带有对象名称时,属性和方法引用才会被正确解析。
关于java - 将对象作为 "root"放入 VelocityContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44693606/