Jos*_*ior 6 java jsp richfaces java-ee jsf-2
我有一个专门用于静态内容的服务器,所以我不想使用资源目录存储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;
}
};
}
Run Code Online (Sandbox Code Playgroud)
通过此更改,我可以将其添加到我的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>
Run Code Online (Sandbox Code Playgroud)
请注意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 />可以读取只有形成你的web应用程序内部本地资源文件夹
你可以做的是使用 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,
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>
Run Code Online (Sandbox Code Playgroud)
通过以上配置,以下资源:
<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" />
Run Code Online (Sandbox Code Playgroud)