Storing web content in a JAR file

后端 未结 6 1681
被撕碎了的回忆
被撕碎了的回忆 2021-02-14 03:16

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 t

相关标签:
6条回答
  • 2021-02-14 03:21

    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.

    0 讨论(0)
  • 2021-02-14 03:26

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

    0 讨论(0)
  • 2021-02-14 03:29

    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.

    0 讨论(0)
  • 2021-02-14 03:31

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

    0 讨论(0)
  • 2021-02-14 03:32

    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" .../>
    
    0 讨论(0)
  • 2021-02-14 03:38

    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.

    0 讨论(0)
提交回复
热议问题