Storing web content in a JAR file

不打扰是莪最后的温柔 提交于 2019-12-03 11:51:32

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.

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

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

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

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.

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.

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