简体   繁体   English

class.getResource返回我文件的错误路径

[英]class.getResource returns a wrong path of my file

I want to use Velocity to template my emails, but i struggled to give the right path of my template : "template.vm" that in my resource folder created by maven. 我想使用Velocity对电子邮件进行模板化,但是我很难在maven创建的资源文件夹中提供模板的正确路径:“ template.vm”。

I used : class.getResource("template.vm").getPath => but getResource retruns null wich is logique cause as i know template.vm in that case should be in the same package of the class, and my template.vm is on WEB-INF/classes 我使用过:class.getResource(“ template.vm”)。getPath =>但getResource retruns null只是逻辑上的原因,因为我知道在这种情况下template.vm应该在该类的同一包中,而我的template.vm是在WEB-INF / classs上

So i used : class.getResource("/template.vm").getPath, but it returns a wrong path, in my case the path of template.vm is : D:/folderOne/FolderTwo/WEB-INF/classes/template.vm and getResource retruns : /D:/folderOne/FolderTwo/WEB-INF/classes/template.vm what makes velocity class to throw : Unable to find resource with the given path : /D:/folderOne/FolderTwo/WEB-INF/classes/template.vm 所以我用了:class.getResource(“ / template.vm”)。getPath,但是它返回了错误的路径,在我的情况下template.vm的路径是:D:/ folderOne / FolderTwo / WEB-INF / classes / template .vm和getResource重新运行:/D:/folderOne/FolderTwo/WEB-INF/classes/template.vm是什么导致速度类抛出的:无法找到具有给定路径的资源:/ D:/ folderOne / FolderTwo / WEB-INF /classes/template.vm

I am struggling with this little probleme more then a day now, please help. 现在,我每天都在为这个小问题而苦苦挣扎,请提供帮助。

You should use ClassPathResourceLoader instead of FileResourceLoader for your Velocity. 对于您的Velocity,应该使用ClassPathResourceLoader而不是FileResourceLoader If you use it, you can simply call VelocityEngine.getTemplate("template.vm") . 如果使用它,则只需调用VelocityEngine.getTemplate("template.vm")

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

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