Java – how to share a JSF error page among multiple Wars

I'm trying to share an error page (error. XHTML) between multiple wars They are all in a big ear application, and they all use a general jar library, I think here

The error page should use web XML or better web fragment XML and will 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)

Simply placing the error page under meta - inf / resource does not work because it is not a resource

I want as few configurations as possible in each war file

I use GlassFish 3.1, but use Java EE 6 standard as much as possible

Solution

You need to create a custom resourceresolver, which can parse resources from the classpath, put them in a common jar file, and then put them in the web-fragment.jar XML (or web. XML for wars)

Startup 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;
    }

}

In Web fragment XML or web In XML

<context-param>
    <param-name>javax.faces.FACELETS_RESOURCE_RESOLVER</param-name>
    <param-value>com.example.FaceletsResourceResolver</param-value>
</context-param>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>