我有一台专用于静态内容的服务器,因此我不希望使用资源目录来存储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>
我该怎么办?
解决方案:
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的相对路径,例如:
<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 />
等使用资源的所有内容;
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;
- José Roberto Araújo Júnior<script type="text/javascript" src="http://cdn.example.com/js/javascript.js"></script>
- José Roberto Araújo Júnior