把java项目部署到tomcat中以后,类文件的路径都在“apache-tomcat-7.0.69\webapps\APP\WEB-INF\classes”中,如果我们需要在某个java类中获取WEB-INF下的某些资源,例如现在我需要在WEB-INF下的templates文件夹下的模板资源

webp

Paste_Image.png

就需要在java文件中这样获取classes目录,然后转到templates目录下。

private static String templatePath = TemplateFactory.class.getClassLoader().getResource("/").getPath().replace("classes", "templates");

其中TemplateFactory是我的Java类名,TemplateFactory.class.getClassLoader().getResource("/").getPath()是获取到classes文件夹下的路径,就是apache-tomcat-7.0.69\webapps\APP\WEB-INF\classes,然后可以把classes替换为我们想要得到的文件夹名字。