我在使用 Apache Velocity 1.7 时没有得到这个信息。 当我有这样的虚拟机时
db.connection.url = $db.customer.environment.db_url
和这样的上下文......
VelocityContext context = new VelocityContext();
context.put("db.customer.environment.db_url", "//sample_db_conn");
我收到此错误
由以下原因引起:org.apache.velocity.exception.MethodInitationException:对象“java.lang.String”在 db.properties.vm[第 2 行,第 42 列]处不包含属性“环境”
但是如果我这样说......它会起作用......
context.put("db.db_url", "//sample_db_conn");
不知道为什么有多个“。”上下文键中的内容导致了此错误。 有什么提示可以克服这个问题吗?
最佳答案
点用作属性访问器。当 Velocity 看到 $db.customer.environment.db_url
时,它会尝试从 db
键下的上下文中获取对象,然后尝试调用 getCustomer( )
或 get("customer")
,依此类推。
因此,对于 Velocity,在按键中使用点是一个非常糟糕的主意 - 但是,有一些解决方法。
您需要将上下文放入其本身,例如:
context.put("context", context);
然后在您的模板中您将能够执行以下操作:
$context.get("db.customer.environment.db_url")
关于java - 为什么 Apache Velocity 无法处理多个点键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51986202/