how to share a jsf error page between multiple wars

北城以北 提交于 2019-11-26 16:30:11

问题


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.


回答1:


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>


来源:https://stackoverflow.com/questions/5379995/how-to-share-a-jsf-error-page-between-multiple-wars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!