我有一个专门用于静态内容的服务器,所以我不想使用资源目录来存储
javascript文件,但我不想停止使用< 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文件中
org.omnifaces.CDN_RESOURCE_HANDLER_URLS
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
请注意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 />,任何使用资源的东西;