java - 将对象作为 "root"放入 VelocityContext

标签 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 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/

相关文章:

java - 编译后的 Eclipse maven 在 Eclipse 中看不到目标/类文件夹

用于 Kafka SQL 的 Java API

liferay - 如何获取 Liferay 速度模板中的页面标题

java - 如何让弹跳球移动得更快?动态速度?

apache - Velocity 找不到资源

java - Apache MetaModel 通过 MongoDB 中的 id 选择

java - Jenkins 不构建 Jasper 报告文件

java - 在多线程应用程序 Android 中访问数据库的最佳方式?

java - 如何使用 Apache Velocity 1.7 保持模板空白(制表符)格式?

java - 使用 Velocity 模板生成的电子邮件中的绝对 URL