简体   繁体   English

让速度在战争中寻找模板

[英]Let velocity find templates in war

I am using Velocity templates which I store in src/main/webapp/templates . 我正在使用存储在src/main/webapp/templates Velocity src/main/webapp/templates

My Jersey-based REST service needs to find these templates. 我基于泽西岛的REST服务需要找到这些模板。 Locally in my Eclipse (with Sysdeo plugin), I used 我在Eclipse本地(使用Sysdeo插件)中使用

p.setProperty("resource.loader", "class");
p.setProperty("class.resource.loader.class",
    "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");

to make Tomcat find the templates (which I got from https://stackoverflow.com/a/38812523/927493 ). 使Tomcat查找模板(我从https://stackoverflow.com/a/38812523/927493获得 )。 Now when I deploy the war to a "real" Tomcat, the resources are not found (Unable to find resource 'templates/FreigabeNeu.vm') 现在,当我部署战争“真正的”雄猫,资源找到(无法找到资源“模板/ FreigabeNeu.vm”)

I probably have to set the properties to something different, but I just don't find how I need to do it. 我可能必须将属性设置为其他内容,但我只是找不到所需的方法。

I checked that the templates directory indeed is copied to the main directory of the war. 我检查了templates目录确实复制到了战争的主目录。

The ClasspathResourceLoader will load resources from the classpath , not just some arbitrary place within a WAR file. ClasspathResourceLoader会从classpath加载资源,而不仅仅是一个WAR文件中的一些任意的地方。 That means that if your templates are in /webapp/templates within your WAR file (or wherever) btu that path isn't in the classpath , then they will not be able to be found. 这意味着,如果您的模板位于WAR文件中的/webapp/templates中(或任何位置),则该路径不在classpath中 ,那么将无法找到它们。

If this is a web application, then what you really want to use is Velocity Tools's WebappLoader which uses the ServletContext to load files from the context itself. 如果这是一个Web应用程序,那么您真正想要使用的是Velocity Tools的 WebappLoader ,它使用ServletContext从上下文本身加载文件。 That is capable of reading files from virtually anywhere in the web application, even from WAR files, etc. 它能够从Web应用程序中的几乎任何位置读取文件,甚至可以从WAR文件等中读取文件。

You might have to double-check to see what the structure of your WAR file – exactly where the templates/ directory ends up – to make sure you have the base path correct. 您可能需要仔细检查以查看WAR文件的结构( templates/目录的确切位置),以确保基本路径正确。 My guess is that they will end up under either /templates or /webapp/templates , but I suspect the former is more likely... the directory webapp is very likely to end up being the root of the WAR file, so /templates is probably correct. 我的猜测是,它们最终将位于/templates/webapp/templates ,但我怀疑前者更有可能...目录webapp很可能最终成为WAR文件的根目录,因此/templates是可能是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM