我该如何使用< h:outputScript />用远程文件? [英] How can I use <h:outputScript /> with a remote file?

查看:211
本文介绍了我该如何使用< h:outputScript />用远程文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个专门用于静态内容的服务器,所以我不想使用资源目录来存储javascript文件,但我不想停止使用< h:outputScript /> tag。

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.

如何让该标签生成指向文件所在的静态服务器的链接,而不是 RES_NOT_FOUND 。我甚至不需要JSF检查文件是否存在...

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...

我试过:< h:outputScript name =# {requestBean.staticURL} /javascript.js/>

生成:< script type = text / javascriptsrc =http://static.server.com/javascript.js>< / script>

但它会生成:< script type =text / javascriptsrc =RES_NOT_FOUND>< / script>

我该怎么办?

解决方案:
Daniel给我一个很好的解决方案!

SOLUTION: Daniel pointed me to a nice solution!

我已经下载了Omnifaces的源代码并修改了 org.omnifaces.resourcehandler.CDNResourceHandle.createResource(String resourceName,String libraryName)方法:

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

通过此更改,我可以将其添加到我的 web.xml file

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>

注意 somelib2:%= http://cdn.example.com / somelib2 ,这会将 somelib2 库中的任何资源指向 http://cdn.example.com/somelib2 中的相对路径,示例:

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.jslibrary =somelib2/>

将输出:

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

这适用于< h:outputScript />< h:outputStylesheet />< h:graphicImage /> ,任何使用资源的内容;

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

推荐答案

你不能

仅仅因为< ; h:outputScript /> 可以在您的网络应用程序中读取表单本地资源文件夹

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

您可以做的是使用 Omnifaces CDNResourceHandler ,这里是 JavaDoc

它允许您使用远程文件

这里展示的一些代码

为了让它运行,这个处理程序需要在faces-config.xml中注册如下:

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

这篇关于我该如何使用&lt; h:outputScript /&gt;用远程文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆