本文介绍了如何对 jar 中的资源使用 JSF 版本控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PF 3.5.10,Mojarra 2.1.21,omnifaces 1.5

PF 3.5.10, Mojarra 2.1.21, omnifaces 1.5

我有一个 JSF 库(只有 css 文件).该库位于 .jar 文件中.css 将包含在 xhtml 中.

I have a JSF library (with css files only). This library is in a .jar file. The css will be included in xhtml with<h:outputStylesheet library="mylib" name="css/mycss.css">.

在 html 中,它呈现为以下内容:localhost:8080/cms/javax.faces.resource/css/mycss.css.jsf?ln=mylib

In html it is rendered to the following: localhost:8080/cms/javax.faces.resource/css/mycss.css.jsf?ln=mylib

primefaces 的 CSS 文件被渲染为:localhost:8080/cms/javax.faces.resource/primefaces.js.jsf?ln=primefaces&v=3.5.10

CSS file of primefaces is rendered to:localhost:8080/cms/javax.faces.resource/primefaces.js.jsf?ln=primefaces&v=3.5.10

请注意最后的库版本 (&3.5.10).我怎么能做同样的事情?我应该在 Manifest.mf 中编写版本吗?或者如何在 jar 文件中使用 jsf-versioning?

Notice the library version (&3.5.10) at the end. How can I do the same thing ? Should I write version in Manifest.mf. Or how can I use jsf-versioning in jar file?

推荐答案

不幸的是,这是不可能的.JAR 中的资源不支持库版本控制.

That's unfortunately not possible. Library versioning is not supported for resources in JAR.

您基本上有两个选择:

  1. 用简单而丑陋的方式来做,包括服务器的启动时间作为查询字符串.鉴于您使用的是 OmniFaces,您可以使用其内置的 #{startup} 托管 bean 来引用应用程序范围内的 java.util.Date 实例:

  1. Do it the easy and ugly way, include server's startup time as query string. Given that you're using OmniFaces, you could use its builtin #{startup} managed bean referring a java.util.Date instance in application scope:

<h:outputStylesheet ... name="some.css?#{startup.time}" />
<h:outputScript ... name="some.js?#{startup.time}" />

或者您可能已经将版本作为某个应用程序变量.

Or perhaps you've the version already as some application variable.

<h:outputStylesheet ... name="some.css?v=#{app.version}" />
<h:outputScript ... name="some.js?v=#{app.version}" />

更新:尽管如此,这对 不起作用.另请参阅:https://github.com/javaserverfaces/mojarra/issues/3945https://github.com/javaee/javaserverfaces-spec/issues/1395

Update: Notwithstanding, this doesn't work for <h:outputStylesheet>. See also: https://github.com/javaserverfaces/mojarra/issues/3945 or https://github.com/javaee/javaserverfaces-spec/issues/1395

它适用于 <h:outputScript>,但它有一个非常相似的错误报告,很快就实现了 https://github.com/javaserverfaces/mojarra/issues/1216

It works for <h:outputScript> though, which had a very simliar bug report which was implemented pretty soon https://github.com/javaserverfaces/mojarra/issues/1216

做同样的事情作为 PrimeFaces,创建一个自定义的 ResourceHandler.

Do the same as PrimeFaces, create a custom ResourceHandler.

public class MyVersionResourceHandler extends ResourceHandlerWrapper {

    private ResourceHandler wrapped;

    public MyVersionResourceHandler(ResourceHandler wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public Resource createResource(String resourceName) {
        return createResource(resourceName, null, null);
    }

    @Override
    public Resource createResource(String resourceName, String libraryName) {
        return createResource(resourceName, libraryName, null);
    }

    @Override
    public Resource createResource(String resourceName, String libraryName, String contentType) {
        final Resource resource = super.createResource(resourceName, libraryName, contentType);

        if (resource == null) {
            return null;
        }

        return new ResourceWrapper() {

            @Override
            public String getRequestPath() {
                return super.getRequestPath() + "&v=1.0";
            }

            @Override // Necessary because this is missing in ResourceWrapper (will be fixed in JSF 2.2).
            public String getResourceName() {
                return resource.getResourceName();
            }

            @Override // Necessary because this is missing in ResourceWrapper (will be fixed in JSF 2.2).
            public String getLibraryName() {
                return resource.getLibraryName();
            }

            @Override // Necessary because this is missing in ResourceWrapper (will be fixed in JSF 2.2).
            public String getContentType() {
                return resource.getContentType();
            }

            @Override
            public Resource getWrapped() {
                return resource;
            }
        };
    }

    @Override
    public ResourceHandler getWrapped() {
        return wrapped;
    }

}

或者,如果您碰巧已经在使用 OmniFaces,则可以更简单地完成:

Or if you happen to already use OmniFaces, it could be done simpler:

public class YourVersionResourceHandler extends DefaultResourceHandler {

    public YourVersionResourceHandler(ResourceHandler wrapped) {
        super(wrapped);
    }

    @Override
    public Resource decorateResource(Resource resource) {
        if (resource == null || !"mylib".equals(resource.getLibraryName())) {
            return resource;
        }

        return new RemappedResource(resource, resource.getRequestPath() + "&v=1.0");
    }

}

无论哪种方式,要让它运行,请在 JAR 的 /META-INF/faces-config.xml 中将其注册为 .

Either way, to get it to run, register it as <resource-handler> in /META-INF/faces-config.xml of the JAR.

<application>
    <resource-handler>com.example.MyVersionResourceHandler</resource-handler>
</application>

这篇关于如何对 jar 中的资源使用 JSF 版本控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 11:16