Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use <h:outputScript /> with a remote file?

I have a server dedicated for static contents so I don't want to use the resources dir for storing javascript files but I don't want to stop using the <h:outputScript /> tag.

How can I make that tag generate a link to my static server where the files are located instead of RES_NOT_FOUND. I don't even need JSF to check if the files are there...

I tried: <h:outputScript name="#{requestBean.staticURL}/javascript.js"/>

To generate: <script type="text/javascript" src="http://static.server.com/javascript.js"></script>

But it generates: <script type="text/javascript" src="RES_NOT_FOUND"></script>

What can I do?

SOLUTION: Daniel pointed me to a nice solution!

I've downloaded the Omnifaces's source code and modified the org.omnifaces.resourcehandler.CDNResourceHandle.createResource(String resourceName, String libraryName) method to:

public Resource createResource(String resourceName, String libraryName) {
    final Resource resource = wrapped.createResource(resourceName, libraryName);

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

    String resourceId = ((libraryName != null) ? libraryName + ":" : "") + resourceName;

    String path = cdnResources.get(resourceId);

    if(path == null){
        if(libraryName != null){
            resourceId = libraryName + ":%";
            path = cdnResources.get(resourceId);
            if(path == null){
                return resource;
            }
            path += "/"+resourceName;
        }
        else return resource;
    }

    final String requestPath = path;

    return new ResourceWrapper() {

        @Override
        public String getRequestPath() {
            return requestPath;
        }

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

With this change I can add this to my web.xml file

<context-param>
    <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_URLS</param-name>
    <param-value>
        somelib2:%=http://cdn.example.com/somelib2,
        js/script1.js=http://cdn.example.com/js/script1.js,
        somelib:js/script2.js=http://cdn.example.com/somelib/js/script2.js,
        otherlib:style.css=http://cdn.example.com/otherlib/style.css,
        images/logo.png=http://cdn.example.com/logo.png
   </param-value>
</context-param>

Notice the somelib2:%=http://cdn.example.com/somelib2, this will point any resource in somelib2 library to the relative path in http://cdn.example.com/somelib2, for example:

<h:outputScript name="js/myjs.js" library="somelib2" />

will output:

<script type="text/javascript" src="http://cdn.example.com/somelib2/js/myjs.js"></script>

This works with <h:outputScript /><h:outputStylesheet /><h:graphicImage />, anything that use resources;

like image 794
José Roberto Araújo Júnior Avatar asked Dec 02 '12 06:12

José Roberto Araújo Júnior


2 Answers

You can't

Simply because <h:outputScript /> can read only form local resource folder inside your web app

What you could do is use Omnifaces CDNResourceHandler , and here is the JavaDoc

It will allow you to use with a remote file

Here some code from the showcase

To get it to run, this handler needs be registered as follows in faces-config.xml:

<application>
    <resource-handler>org.omnifaces.resourcehandler.CDNResourceHandler</resource-handler>
</application>



<context-param>
    <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_URLS</param-name>
    <param-value>
        js/script1.js=http://cdn.example.com/js/script1.js,
        somelib:js/script2.js=http://cdn.example.com/somelib/js/script2.js,
        otherlib:style.css=http://cdn.example.com/otherlib/style.css,
        images/logo.png=http://cdn.example.com/logo.png
    </param-value>
</context-param>

With the above configuration, the following resources:

<h:outputScript name="js/script1.js" />
<h:outputScript library="somelib" name="js/script2.js" />
<h:outputStylesheet library="otherlib" name="style.css" />
<h:graphicImage name="images/logo.png" />
like image 109
Daniel Avatar answered Sep 18 '22 23:09

Daniel


I think you don't need that. It is used to display a script taken from archives in your classpath. For a regular script you can simply type the corresponding <script> tag.

like image 21
Bozho Avatar answered Sep 20 '22 23:09

Bozho