Search code examples
javajsfjakarta-eejsf-2facelets

how to share a jsf error page between multiple wars


I'm trying to share an error page (error.xhtml) between multiple wars. They are all in a big ear application, and all use a common jar library, where I'd like to put this.

The error page should use web.xml, or better web-fragment.xml, and would be declared as a standard java ee error page.

Actual EAR structure:

EAR
 EJB1
 EJB2
 WAR1 (using CommonWeb.jar)
 WAR2 (using CommonWeb.jar)
 WAR3 (using CommonWeb.jar)

Just putting the error page under META-INF/resources won't work, as it's not a resource.

I'd like to have as little as possible to configure in each war file.

I'm using Glassfish 3.1, but would like to use Java EE 6 standards as much as possible.


Solution

  • You need to create a custom ResourceResolver which resolves resources from classpath, put it in the common JAR file and then declare it in web-fragment.xml of the JAR (or in web.xml of the WARs).

    Kickoff example:

    package com.example;
    
    import java.net.URL;
    
    import javax.faces.view.facelets.ResourceResolver;
    
    public class FaceletsResourceResolver extends ResourceResolver {
    
        private ResourceResolver parent;
        private String basePath;
    
        public FaceletsResourceResolver(ResourceResolver parent) {
            this.parent = parent;
            this.basePath = "/META-INF/resources"; // TODO: Make configureable?
        }
    
        @Override
        public URL resolveUrl(String path) {
            URL url = parent.resolveUrl(path); // Resolves from WAR.
    
            if (url == null) {
                url = getClass().getResource(basePath + path); // Resolves from JAR.
            }
    
            return url;
        }
    
    }
    

    with in web-fragment.xml or web.xml

    <context-param>
        <param-name>javax.faces.FACELETS_RESOURCE_RESOLVER</param-name>
        <param-value>com.example.FaceletsResourceResolver</param-value>
    </context-param>