2012-12-02 24 views
5

我有一個專用於靜態內容的服務器,所以我不想使用資源目錄來存儲JavaScript文件,但我不想停止使用<h:outputScript />標記。如何將<h:outputScript />與遠程文件一起使用?

如何使該標記生成指向我的靜態服務器的文件所在的位置而不是RES_NOT_FOUND。我甚至都不需要JSF檢查文件是否存在...

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

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

但它產生:<script type="text/javascript" src="RES_NOT_FOUND"></script>

我能做些什麼?

解決方案: 丹尼爾指出我一個很好的解決方案!

我已經下載了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,例如:

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

將輸出:

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

這適用於<h:outputScript /><h:outputStylesheet /><h:graphicImage />,任何使用資源;

回答

8

不能

很簡單,因爲<h:outputScript />可以閱讀只有形式的本地資源文件夾中的Web應用程序內

你可以做的是使用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" /> 
+0

謝謝你的建議幫了我很多,解決了我的問題:) 我改變了CDNResourceHandle.createResource(字符串資源名稱, String libraryName)方法允許使用庫鏈接到我的服務器,而不是列出每個文件,現在它的工作完美:) –

+0

如果您對更改感興趣,我更改了這部分: 'String resourceId =( (libraryName!= null)?libraryName +「:」:「」)+ resourceName; String path = cdnResources.get(resourceId); if(path == null){if(libraryName!= null){ \t \t resourceId = libraryName +「:%」;如果(path == null){ \t if \t \t path = cdnResources.get(resourceId); \t \t if(path == null){ \t \t \t return resource; \t \t} \t \t path + =「/」+ resourceName; \t} \t else return resource; } 最終字符串requestPath =路徑;' –

+0

這將完成這項工作:'<的context-param> ​​org.omnifaces.CDN_RESOURCE_HANDLER_URLS alibraryname:%= HTTP://cdn.example.com ',現在你可以這樣做:'' –

1

我認爲你不需要那樣。它用於顯示從類路徑中的檔案中獲取的腳本。對於常規腳本,您只需鍵入相應的<script>標籤即可。

相關問題