我想使用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"/>
在检查了这种情况的输出之后,我看到combinend脚本只按顺序包含第一个脚本,控制台显示“ReferenceError:OmniFaces未定义”:
<body>
<script type="text/javascript">OmniFaces.DeferredScript.add('/testApp/javax.faces.resource/eNpLL81JLE7OsMoq1s8qLE0tqoRSermZeXpZxQDDagwa.js?ln=omnifaces.combined&v=0');</script>
</body>
我注意到,当jsf.js
活跃时,即使CombinedResourceHandler
也不包括在内。浏览器控制台告诉“mojarra未定义”。
我究竟做错了什么?提前致谢!
我的环境是:Mojarra 2.2.12,Omnifaces 2.5.1,Tomcat 8。
上周末我复制了一个非常相似的issue。原因归结为Mojarra在Tomcat 8服务器上初始化了两次,从而破坏了其中一个。您可以通过查看服务器日志来确认这一点,并注意其中Mojarra版本,OmniFaces版本和PrimeFaces版本都会记录两次。
如果您只有一个Mojarra实例并且您没有ConfigureListener
中的web.xml
条目,请加倍验证,如下所示,因为它默认已自动注册。
<!-- 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>
对于遇到此问题的任何其他人,我也使用Jboss EAP 7.1遇到了这个问题。
我不小心在Web Fragment JAR的faces-config.xml和Web应用程序本身的faces-config.xml中声明了OmniFacesCombinedResourceHandler。声明两次导致与上面列出的问题相同的症状。一旦我从webapp faces-config.xml中删除它,它就开始工作了。
我提出了关于OmniFaces问题的问题,如果发生这种情况可能会检测并引发错误:https://github.com/omnifaces/omnifaces/issues/504