我想使用 OmniFaces CombinedResourceHandler
一次性流式传输资源。
我在faces-config.xml
中注册了它,没有任何其他配置参数,如 CombinedResourceHandler documentation 中所述。 .
虽然它对 CSS 资源工作得很好,但对 JavaScript 资源却没有任何作用。这是我的测试:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com /jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:o="http://omnifaces.org/ui">
<h:head>
<title>CombinedResourceHandlerTest</title>
<h:outputStylesheet name="css/bootstrap-3.3.5/bootstrap.css"/>
<h:outputStylesheet name="css/main.css" />
<h:outputScript name="js/jquery/jquery.min.js"/>
<h:outputScript name="js/bootstrap-3.3.5/bootstrap.min.js"/>
</h:head>
<h:body>
<f:view>
<h2>CombinedResourceHandlerTest</h2>
</f:view>
</h:body>
输出:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="j_idt2">
<title>CombinedResourceHandlerTest</title>
<script type="text/javascript" src="/testApp/javax.faces.resource/js/jquery/jquery.min.js"></script>
<script type="text/javascript" src="/testApp/javax.faces.resource/js/bootstrap-3.3.5/bootstrap.min.js"></script>
<link type="text/css" rel="stylesheet" href="/testApp/javax.faces.resource/eNpLLi7WT8rPLykuKUos0DXWM9YzRfD1kouLa4BYPzcxMw_EAQCLpxEP.css?ln=omnifaces.combined&v=1480321351184">
</head>
尝试使用属性 target="head":
<h:head>
<h:outputScript name="js/jquery/jquery.min.js" target="head"/>
</h:head>
...
输出:(脚本完全丢失):
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="j_idt2">
<title>CombinedResourceHandlerTest</title>
<link type="text/css" rel="stylesheet" href="/testApp/javax.faces.resource/eNpLLi7WT8rPLykuKUos0DXWM9YzRfD1kouLa4BYPzcxMw_EAQCLpxEP.css?ln=omnifaces.combined&v=1480321351184">
</head>
...
</html>
当我将它们移动到正文顶部时,脚本也会丢失:
<h:body>
<h:outputScript name="js/jquery/jquery.min.js" target="head"/>
....
</h:body>
查看 source 后我也尝试过
<o:deferredScript name="js/jquery/jquery.min.js"/>
检查此情况的输出后,我发现组合脚本仅包含按顺序的第一个脚本,并且控制台显示“ReferenceError:OmniFaces未定义”:
<body>
<script type="text/javascript">OmniFaces.DeferredScript.add('/testApp/javax.faces.resource/eNpLL81JLE7OsMoq1s8qLE0tqoRSermZeXpZxQDDagwa.js?ln=omnifaces.combined&v=0');</script>
</body>
我注意到,当 CombinedResourceHandler
处于事件状态时,甚至不包括 jsf.js
。浏览器控制台告诉“mojarra 未定义”。
我做错了什么?提前致谢!
我的环境是:Mojarra 2.2.12、Omnifaces 2.5.1、Tomcat 8。
最佳答案
我复制了一个非常相似的issue上周末。原因归结为 Mojarra 在 Tomcat 8 服务器上初始化了两次,从而损坏了其中一个服务器。您可以通过查看服务器日志来确认这一点,并注意到 Mojarra 版本、OmniFaces 版本和 PrimeFaces 版本被记录两次。
如果您只有一个 Mojarra 实例,并且您在 web.xml
中没有有 ConfigureListener
条目,请仔细验证,如下所示默认情况下它已经自动注册。
<!-- You MUST remove this one from web.xml! -->
<!-- This is actually a workaround for buggy GlassFish3 and Jetty servers. -->
<!-- When leaving this in and you're targeting Tomcat, you'll run into trouble. -->
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
另请参阅:
关于jsf - OmniFaces CombinedResourceHandler 不合并 JavaScript 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40948684/