【问题标题】:Is Primefaces able to get jquery from another domain?Primefaces 是否能够从另一个域获取 jquery?
【发布时间】:2013-03-26 21:40:56
【问题描述】:

我在我的 JSF2 应用程序中的一些页面上使用 primefaces。我想控制页面从哪里获取 jquery.js。有没有办法在 faces-config 或 web.xml 中指定不添加 JQuery javascript 库。

例如不加:

<script type="text/javascript" src="/myappcontextroot/javax.faces.resource/jquery/jquery.js.jsf?ln=primefaces"></script>

我希望页面输出类似于:

<script type="text/javascript" src="http://mydomain.com/jquery/jquery.js"></script>

或者在需要 jquery 库时不做任何输出。 (我会手动将以上内容添加到页面中。)

这甚至可能吗?如果有,怎么做?

【问题讨论】:

    标签: jquery jsf jsf-2 primefaces


    【解决方案1】:

    您基本上需要一个自定义的resource handler,只要请求资源primefaces:jquery/jquery.js,它就会在Resource#getRequestPath() 上返回所需的外部URL。

    例如

    public class CDNResourceHandler extends ResourceHandlerWrapper {
    
        private ResourceHandler wrapped;
    
        public CDNResourceHandler(ResourceHandler wrapped) {
            this.wrapped = wrapped;
        }
    
        @Override
        public Resource createResource(final String resourceName, final String libraryName) {
            final Resource resource = super.createResource(resourceName, libraryName);
    
            if (resource == null || !"primefaces".equals(libraryName) || !"jquery/jquery.js".equals(resourceName)) {
                return resource;
            }
    
            return new ResourceWrapper() {
    
                @Override
                public String getRequestPath() {
                    return "http://mydomain.com/jquery/jquery.js";
                }
    
                @Override
                public Resource getWrapped() {
                    return resource;
                }
            };
        }
    
        @Override
        public ResourceHandler getWrapped() {
            return wrapped;
        }
    
    }
    

    要让它运行,请将其映射到faces-config.xml,如下所示:

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

    JSF 实用程序库OmniFaces 提供了CDNResourceHandler 风格的可重用解决方案,在您的情况下配置为

    <context-param>
        <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_URLS</param-name>
        <param-value>primefaces:jquery/jquery.js=http://mydomain.com/jquery/jquery.js</param-value>
    </context-param>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-04
      • 2014-04-10
      • 1970-01-01
      相关资源
      最近更新 更多