Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将对象放置为";根“;到VelocityContext_Java_Velocity - Fatal编程技术网

Java 将对象放置为";根“;到VelocityContext

Java 将对象放置为";根“;到VelocityContext,java,velocity,Java,Velocity,我想访问模板中某个模型类的所有属性。如果我这样做 VelocityContext context = new VelocityContext(); context.put("model", new MyModel()); 而MyModel就像 public class MyModel { private String propertyA; private String propertyB; public String getPropertyA() { return pr

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

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中是不可能的-无法指定“默认”对象。属性和方法引用只有在前面加上其对象的名称时才能正确解析。

检查此链接:@soorapadman谢谢,我以前读过,我认为这些是不同的主题。好的。我认为这可能会帮助您。抱歉。这在Velocity中是不可能的-无法指定“默认”对象。属性和方法引用只有在前面加上其对象的名称时才能正确解析。@MarkusFischer感谢您的回答,因为这是对我问题的有效回答,您是否可以将其作为正确答案发布,并允许我接受它。:)