我正在尝试将项目从 Spring 3 升级到 Spring 5.0.5。我用我编写的新初始化更新了速度:
private static void initVelocity() throws VelocityException, IOException{
Properties props = new Properties();
props.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
props.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
Velocity.init( props );
}
我正在使用以下依赖项:
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>
但是由于一个奇怪的原因,当我执行 Velocity.getProperty( "classpath.resource.loader.class");
时,我得到一个 null
,并且我得到一个当我尝试获取模板时,NullPointerException
。
最佳答案
我不太熟悉 Velocity,但我们通常会创建一个新的 VelocityEngine
实例。您可以从特定实例而不是 Velocity 对象本身获取该属性。请参阅以下代码:
Properties props = new Properties()
props.put("classpath.resource.loader.class",
ClasspathResourceLoader.class.getName());
props.put(RuntimeConstants.RESOURCE_LOADER, "classpath");
VelocityEngine engine = new VelocityEngine(props);
现在通过
获取加载程序engine.getProperty("classpath.resource.loader.class");
关于java - Velocity 2 不会设置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50218020/