类路径中未找到 Velocity 模板

发布于 2024-12-20 05:17:30 字数 855 浏览 2 评论 0原文

当我切换到“类路径”资源加载器时,我似乎无法访问我的速度模板。我尝试将 templates 目录放在 /WEB-INF/classes/templates、/WEB-INF/templates 上,在 /WEB-INF/lib 中创建 templates.jar。它们都不起作用。有什么想法吗?这些文件的权限都是正确的。

Properties p = new Properties();
p.setProperty("runtime.log.logsystem.class", "org.apache.velocity.tools.generic.log.CommonsLogLogSystem");

/*
// Works fine:
p.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
p.setProperty("file.resource.loader.path", "/path/to/templates");
*/

// Cannot find template with this:
p.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
p.setProperty("class.resource.loader.class", ClasspathResourceLoader.class.getName());
p.setProperty("resourceLoaderPath", "/WEB-INF/classes/templates");

org.apache.velocity.app.Velocity.init(p);

template = org.apache.velocity.app.Velocity.getTemplate("confirmation_html.vm");

I cannot seem to get access to my velocity templates when I switch to the "classpath" resource loader. I've tried putting a templates directory on /WEB-INF/classes/templates, /WEB-INF/templates, creating a templates.jar in /WEB-INF/lib. None of them work. Any ideas? Permissions on those files are all correct.

Properties p = new Properties();
p.setProperty("runtime.log.logsystem.class", "org.apache.velocity.tools.generic.log.CommonsLogLogSystem");

/*
// Works fine:
p.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
p.setProperty("file.resource.loader.path", "/path/to/templates");
*/

// Cannot find template with this:
p.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
p.setProperty("class.resource.loader.class", ClasspathResourceLoader.class.getName());
p.setProperty("resourceLoaderPath", "/WEB-INF/classes/templates");

org.apache.velocity.app.Velocity.init(p);

template = org.apache.velocity.app.Velocity.getTemplate("confirmation_html.vm");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゝ偶尔ゞ 2024-12-27 05:17:30

这就是我过去的做法。它确实有效,尽管可能不是让它发挥作用的最佳方法。假设您有一个 /webapps/WEB-INF/ 结构,

 Properties prop = new Properties();
 String absolutePath=new File(Thread.currentThread().getContextClassLoader().getResource("").getFile()).getParentFile().getParentFile().getPath();//this goes to webapps directory
 prop.put("file.resource.loader.path", absolutePath+"/WEB-INF/classes/templates");
 Velocity.init(prop);
 Template t=Velocity.getTemplate("confirmation_html.vm");

p1ng

This i how i have done it in the past. It has worked, albeit may not be the best way to get it working. Assuming that you have a /webapps/WEB-INF/ structure,

 Properties prop = new Properties();
 String absolutePath=new File(Thread.currentThread().getContextClassLoader().getResource("").getFile()).getParentFile().getParentFile().getPath();//this goes to webapps directory
 prop.put("file.resource.loader.path", absolutePath+"/WEB-INF/classes/templates");
 Velocity.init(prop);
 Template t=Velocity.getTemplate("confirmation_html.vm");

p1ng

面犯桃花 2024-12-27 05:17:30

IIRC,WEB-INF/classes 是类路径三的根,所以你可以尝试“templates/”或“/templates”

IIRC, WEB-INF/clases is a root of classpath three, so you can just try "templates/" or "/templates"

可爱咩 2024-12-27 05:17:30

这很痛苦。如果您放入类路径,那么开发就会变得地狱,因为每次您在速度模板中进行更改时,Servlet 容器都会重新加载 Web 应用程序。

我建议使用 org.apache.velocity.tools.view.WebappResourceLoader,它不需要文件位于类路径中,从而使开发变得更加容易,并且还允许您执行相对包含。

您还可以查看我的帖子: Spring-mvc + Velocity + DCEVM

This is painful. If you put in the classpath then the development becomes hell, since the servlet container would reload the webapp every time you make a change in the velocity templates.

I would recommend using the org.apache.velocity.tools.view.WebappResourceLoader, which makes development much easier by not requiring the files to be in the classpath and also allows you to do relative includes.

You can also check my post about this: Spring-mvc + Velocity + DCEVM

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文