如何使该标记生成指向文件所在的静态服务器的链接,而不是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 />,任何使用资源的东西;
解决方法
仅仅因为< h:outputScript />只能在您的网络应用程序中读取表单本地资源文件夹
你能做的就是使用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,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" />