how to share a jsf error page between multiple wars [duplicate]
Asked Answered
C

1

13

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.

Castra answered 21/3, 2011 at 15:40 Comment(0)
S
21

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>
Sheathing answered 21/3, 2011 at 16:18 Comment(8)
Can we put that in web-fragment.xml of common library jar, so that I don't need to define it in every jar?Castra
Ok. It works when configured in web.xml, not in web-fragment.xml . web-fragment.xml is in META-INF/Castra
Your answer basically is correct. My web-fragment.xml does not seem to have any effect, but that's another question ;-)Castra
FYI, it has been working in web-fragment.xml for about 6 months now. I had various issues (version nr, etc.) which have been solved since then.Castra
How can this error.xhtml be used from the wars? i.e. <ui:include src="<what should I put here?>/errorPopUp.xhtml" />Solleret
@Pau: Just treat content of /META-INF/resources like as public webcontent.Sheathing
Is there any better way to go within newer versions of JSF and frameworks?Precautionary
@JosePerez: yes, the ResourceResolver is deprecated since JSF 2.2 in favor of ResourceHandler. See stackoverflow.com/a/13296727 for another example.Sheathing

© 2022 - 2024 — McMap. All rights reserved.