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

纵饮孤独 提交于 2020-01-01 09:57:13

问题


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;


回答1:


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" />



回答2:


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.



来源:https://stackoverflow.com/questions/13667052/how-can-i-use-houtputscript-with-a-remote-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!