Storing web content in a JAR file

杀马特。学长 韩版系。学妹 提交于 2019-12-21 03:52:51

问题


Is it possible to store web content (such as JSPs, HTML, images, CSS etc) in a JAR file?

I've been looking at various options at modularising our web applications and this is one possibility.

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! :)

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.

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?


回答1:


If you are using Maven to build your webapp, you can build a WAR of your resources and overlay that WAR onto your webapp WAR at build time.

The resource WAR containing all of your JSPs, images, CSS, etc. is referred to as an "overlay," and is simply a dependency in your target webapp with the type set to "war."

When you package your webapp, the resource WAR will only copy over non-conflicting files. So, if you have a unique index.jsp in your project, and would like to use that instead of the index.jsp in the overlay, just include it in your target webapp, and Maven will not copy over that resource.

More info on the Maven War plugin page about overlays.




回答2:


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.

To load a resource from your deployment jar, use the following code.

this.getClass().getClassLoader().getResourceAsStream( filename ) ;

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".




回答3:


Absolutely. Heck, you can store content directly in a WAR file, which is basically a JAR file with a few extra bits. Yes, you may need to write a custom resolver to use ClassLoader.getResourceAsStream, but basically as you're given the ability to generate the content however you like, fetching it from a jar file seems perfectly reasonable. You'll probably want to make sure it only fetches a very specific set of extensions though :)




回答4:


You can also use the weblets project (see https://weblets.dev.java.net/).

You store some resources in a JAR library (such as images, css, javascript...) and you write a really simple weblet-config.xml. Then in the JSF page, you can refer them directly with this syntax:

<h:graphicImage src="weblet://some-name/images/someimage.jpg" .../>



回答5:


A tag file is like a JSP fragment that can be placed in a jar. Using tag files, could help you, but I have never tried to use images, CSS, etc. in a jar.




回答6:


In Core JavaServer Faces, 3rd edition, under "Packaging Composite Components in JARs" on p. 382, it talks about packaging composite components in JAR files.

"All you have to do is put your composite component, and its artifacts, such as JavaScript, stylesheets, or properties files, under a META-INF directory in the JAR, as shown in Figure 9-14."

components.jar
+-- META-INF
    +-- resources
        +-- css
        |   +-- styles.css
        +-- images
        |   +-- back.png
        +-- util
            +-- icon.xhtml
            +-- login.js
            +-- login.properties

I'm not sure how easily these resources can be accessed directly from other applications as opposed to the contained composite components.



来源:https://stackoverflow.com/questions/200319/storing-web-content-in-a-jar-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!