Can Primefaces get jquery from another domain?

I use JSF2 for some of my pages in my application. I would like to control where the page gets jquery.js. Is there a way to specify in faces-config or web.xml so as not to add jQuery Javascript libraries.

For example, do not add:

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

I would prefer the page to output something like:

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

Or you don’t need to output anything to the jquery library if necessary. (I will manually add this to the page.)

Is it possible? If so, how?

+5
source share
1 answer

Basically, you need a custom resource handler that returns the desired external URL Resource#getRequestPath()whenever a resource is requested primefaces:jquery/jquery.js.

eg.

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>
+8

All Articles