我有一個專用於靜態內容的服務器,所以我不想使用資源目錄來存儲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 />
,任何使用資源;
謝謝你的建議幫了我很多,解決了我的問題:) 我改變了CDNResourceHandle.createResource(字符串資源名稱, String libraryName)方法允許使用庫鏈接到我的服務器,而不是列出每個文件,現在它的工作完美:) –
如果您對更改感興趣,我更改了這部分: '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 =路徑;' –
這將完成這項工作:'<的context-param> org.omnifaces.CDN_RESOURCE_HANDLER_URLS alibraryname:%= HTTP://cdn.example.com context-param>',現在你可以這樣做:' ' –