问题描述
是否可以将Web内容(例如JSP,HTML,图像,CSS等)存储在JAR文件中?
Is it possible to store web content (such as JSPs, HTML, images, CSS etc) in a JAR file?
我一直在寻找各种选项模块化我们的Web应用程序,这是一种可能性。
I've been looking at various options at modularising our web applications and this is one possibility.
我们目前正在使用JSF和Facelets来实现我们的视图技术 - 我认为可以写一些表格自定义视图解析器,它将检查类路径而不是文件系统目录,但我不确定这是否可行。
We are currently using JSF and Facelets for our view technology - I'm thinking it may be possible to write some form of custom view resolver which would examine the classpath rather than a filesystem directory, but I'm not sure this would work.
任何想法都将不胜感激! :)
Any ideas would be appreciated! :)
更新:我应该澄清一下。如何从Web容器(如Tomcat)加载JAR文件中的资源?例如,我使用我的Web应用程序部署.war文件。如果我访问/index.jsp,容器将尝试在web内容目录中查找名为index.jsp的文件。
Update: I should probably clarify. How do you get the web container (such as Tomcat) to load resources from a JAR file? For example, I deploy a .war file with my web application. If I access /index.jsp, the container will try to look in the web content directory for a file named index.jsp.
是否有一种简单的方法来配置你的使用Tomcat等自己的资源加载器,以便它搜索类路径以及文件系统?
Is there an easy way to configure your own resource loader using Tomcat or the like so that it searches the classpath as well as the filesystem?
推荐答案
是的,它是可能的存储文件,例如属性,xml,xslt,图像等;在JAR(或WAR)文件中并在运行时拉它们。
Yes, it is possible to store files e.g. properties, xml, xslt, image etc; in a JAR (or WAR) file and pull them at runtime.
要从部署jar加载资源,请使用以下代码。
To load a resource from your deployment jar, use the following code.
this.getClass().getClassLoader().getResourceAsStream( filename ) ;
在maven项目中,文件夹&放在资源中的文件包含在jar中。文件名相对于jar文件的根目录,因此./filename.xml将匹配/ src / java / resources中的文件filename.xml。
In a maven project, folders & files placed in resources are included in the jar. The filename is relative to the root of jar file, so "./filename.xml" would match the file filename.xml placed in "/src/java/resources".
这篇关于将Web内容存储在JAR文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!