您基本上需要一个自定义资源处理程序Resource#getRequestPath()
,该处理程序在请求资源时返回所需的外部 URL primefaces:jquery/jquery.js
。
例如
public class CDNResourceHandler extends ResourceHandlerWrapper {
private ResourceHandler wrapped;
public CDNResourceHandler(ResourceHandler wrapped) {
this.wrapped = wrapped;
}
@Override
public Resource createResource(final String resourceName, final String libraryName) {
final Resource resource = super.createResource(resourceName, libraryName);
if (resource == null || !"primefaces".equals(libraryName) || !"jquery/jquery.js".equals(resourceName)) {
return resource;
}
return new ResourceWrapper() {
@Override
public String getRequestPath() {
return "http://mydomain.com/jquery/jquery.js";
}
@Override
public Resource getWrapped() {
return resource;
}
};
}
@Override
public ResourceHandler getWrapped() {
return wrapped;
}
}
要让它运行,请将其映射faces-config.xml
如下:
<application>
<resource-handler>com.example.CDNResourceHandler</resource-handler>
</application>
JSF 实用程序库OmniFaces提供了一个可重用的解决方案,CDNResourceHandler
在您的情况下可以将其配置为
<context-param>
<param-name>org.omnifaces.CDN_RESOURCE_HANDLER_URLS</param-name>
<param-value>primefaces:jquery/jquery.js=http://mydomain.com/jquery/jquery.js</param-value>
</context-param>