【问题标题】:How can I use <h:outputScript /> with a remote file?如何将 <h:outputScript /> 与远程文件一起使用?
【发布时间】:2012-11-19 23:06:13
【问题描述】:

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

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

我试过了:&lt;h:outputScript name="#{requestBean.staticURL}/javascript.js"/&gt;

要生成:&lt;script type="text/javascript" src="http://static.server.com/javascript.js"&gt;&lt;/script&gt;

但它会生成:&lt;script type="text/javascript" src="RES_NOT_FOUND"&gt;&lt;/script&gt;

我能做什么?

解决方案: Daniel 为我指出了一个不错的解决方案!

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

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 文件中

<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 中的相对路径,例如:

&lt;h:outputScript name="js/myjs.js" library="somelib2" /&gt;

将输出:

&lt;script type="text/javascript" src="http://cdn.example.com/somelib2/js/myjs.js"&gt;&lt;/script&gt;

这适用于&lt;h:outputScript /&gt;&lt;h:outputStylesheet /&gt;&lt;h:graphicImage /&gt;,任何使用资源的东西;

【问题讨论】:

    标签: java jsp jakarta-ee jsf-2 richfaces


    【解决方案1】:

    你不能

    仅仅是因为&lt;h:outputScript /&gt; 可以从您的网络应用程序内的本地资源文件夹中读取

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

    它将允许您使用远程文件

    这里是展示中的一些代码

    要让它运行,这个处理程序需要在 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>
    

    通过以上配置,以下资源:

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

    【讨论】:

    • 谢谢,您的建议对我帮助很大,解决了我的问题 :) 我更改了 CDNResourceHandle.createResource(String resourceName, String libraryName) 方法以允许使用库链接到我的服务器而不是列出每一个文件,现在它工作得很好:)
    • 如果您对更改感兴趣,我已经更改了这部分: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;
    • 这样就可以了:&lt;context-param&gt; &lt;param-name&gt;org.omnifaces.CDN_RESOURCE_HANDLER_URLS&lt;/param-name&gt; &lt;param-value&gt; alibraryname:%=http://cdn.example.com &lt;/param-value&gt; &lt;/context-param&gt;,现在你可以这样做了:&lt;h:outputScript name="js/javascript.js" library="alibraryname" /&gt;
    • 结果将是:&lt;script type="text/javascript" src="http://cdn.example.com/js/javascript.js"&gt;&lt;/script&gt;
    【解决方案2】:

    我认为你不需要那个。它用于显示从类路径中的档案中获取的脚本。对于常规脚本,您只需键入相应的&lt;script&gt; 标记即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-09
      • 2016-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多