如何从公共目录加载JSF/facelets库? [英] How to load JSF / facelets library from a common directory?

查看:129
本文介绍了如何从公共目录加载JSF/facelets库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有许多Web应用程序都使用某个JSF库.我们希望将库放置在公共位置或主类路径上.问题在于系统希望该库位于WEB-INF/lib中,以便webapp类加载器可以对其进行扫描和加载.

We have a number of webapps that all use a certain JSF library. We'd like to place the library in a common location or on the main classpath. The trouble is that the system expects the library to be in WEB-INF/lib so the webapp classloader can scan it and load it.

我们是这个特定库的创建者,在开发过程中,将其放在主类路径上更加容易.

We are the creators of this particular library, and during development it's way easier to have it on the main classpath.

这可能吗?任何人都可以举一个例子来说明如何将web.xml放入该容器中(如果这是正确的机制)?

Is this possible? Can anyone give an example of what to put in web.xml to make this happen (if that's the right mechanism)?

推荐答案

我们是这个特定库的创建者,在开发过程中,将其放在主类路径上更加容易.这可能吗?

对于Facelets资源,可以使用自定义

For Facelets resources this is possible with a custom ResourceResolver in webapp itself.

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 which would also do META-INF/resources of JARs in WAR.

        if (url == null) {
            url = getClass().getResource("/" + basePath + path); // Resolves from JARs in WAR when base path is not META-INF/resources.
        }

        if (url == null) {
            url = Thread.currentThread().getContextClassLoader().getResource(basePath + path); // Resolves also from anywhere else in classpath.
        }

        return url;
    }

}

要使其运行,请在webapp的web.xml中对其进行如下配置:

To get it to run, configure it as follows in webapp's web.xml:

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

对于需要由JSF注释扫描程序扫描的带注释的JSF工件(例如托管Bean等),这是不可能的. JAR确实必须以/WEB-INF/lib结尾.另请参见如何引用JAR文件中提供的JSF托管bean?

For annotated JSF artifacts like managed beans, etc which needs to be scanned by the JSF annotation scanner, this is not possible. The JAR has really to end up in /WEB-INF/lib. See also How to reference JSF managed beans which are provided in a JAR file?

这篇关于如何从公共目录加载JSF/facelets库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆