我正在尝试在我的项目中使用速度。我指的是 here 中的代码。我的代码除了创建一个带有消息的 html 页面外没有做太多事情。
public static void main(String[] args) {
VelocityEngine ve = new VelocityEngine();
ve.init();
Template t = ve.getTemplate("seat.html");
VelocityContext vc = new VelocityContext();
vc.put("message", "Hello Velocity");
StringWriter sw = new StringWriter();
t.merge(vc, sw);
System.out.println(sw);
}
我的模板文件 seat.html
与包含此方法的类位于同一文件夹中,并且位于项目的 src 文件夹中。这不起作用,所以我尝试使用绝对路径名来引用。但这也不起作用。我还尝试使用 FilePathLoader
和 Properties
配置速度,但我认为这个简单的示例应该像 tutorial 中所示那样工作。 (如上所述)。
文件夹结构:
velocity-test
`-- src
|-- bean
|-- core
| |-- Application.java
| `-- seat.html
`-- seat.html
异常(exception):
SEVERE: ResourceManager : unable to find resource 'seat.html' in any resource loader.
Exception in thread "main" org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'seat.html'
at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:474)
at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:352)
at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1533)
at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1514)
at org.apache.velocity.app.VelocityEngine.getTemplate(VelocityEngine.java:373)
at core.Application.main(Application.java:16)
关于 SO 的问题很少有答案,但所有这些问题都使用一些属性来配置速度引擎;这是我不想要的。
提前致谢!
最佳答案
只需更改为
ve.getTemplate("./src/seat.html");
关于java - Apache 速度 : ReourceNotFoundException in simple example,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35403173/