Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Primefaces able to get jquery from another domain?

I am using primefaces for some of my pages in my JSF2 app. I would like to control where the page gets the jquery.js from. Is there a way to specify in the faces-config or web.xml to not add the JQuery javascript libraries.

For example don't add:

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

I would prefer the page output something like:

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

Or not do output anything when needed the jquery library. (I will manually add the above to the page.)

Is this even possible? If so, how?

like image 212
JeffJak Avatar asked Apr 04 '13 17:04

JeffJak


1 Answers

You basically need a custom resource handler which returns the desired external URL on Resource#getRequestPath() whenever the resource primefaces:jquery/jquery.js is been requested.

E.g.

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;
    }

}

To get it to run, map it in faces-config.xml as follows:

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

The JSF utility library OmniFaces offers a reusable solution in flavor of CDNResourceHandler which is in your case to be configured as

<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>
like image 120
BalusC Avatar answered Nov 15 '22 08:11

BalusC