如何在多次战争之间共享一个jsf错误页面

如何在多次战争之间共享一个jsf错误页面

问题描述:

我正在尝试在多次战争之间共享错误页面(error.xhtml)。它们都在一个大耳朵应用程序中,并且都使用一个通用的jar库,我想把它放在这里。

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.

错误页面应该使用web.xml,或更好的web-fragment.xml,并将被声明为标准的java ee错误页面。

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

实际EAR结构:

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

将错误页面放在META-INF / resources下面不会工作,因为它不是资源。

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

我希望尽可能少地配置每个war文件。

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

我正在使用Glassfish 3.1,但希望尽可能使用Java EE 6标准。

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

您需要创建自定义 ResourceResolver 解析来自classpath的资源,将其放在公共JAR文件中,然后在JAR的 web-fragment.xml 中声明它(或者在 web.xml 的WAR)。

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

开球示例:

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 web.xml

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