问题描述
我正在尝试在多个战争之间共享一个错误页面 (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 结构:
Actual EAR structure:
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.
我希望在每个战争文件中配置尽可能少.
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
从类路径解析资源,将其放入公共 JAR 文件中,然后在 web-fragment.xml 中声明
的 JAR(或在 WAR 的 web.xml
中).
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;
}
}
在 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>
这篇关于如何在多个战争之间共享一个 jsf 错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!