OmniFaces CombinedResourceHandler 不合并 JavaScript 资源

OmniFaces CombinedResourceHandler does not combine JavaScript resources

我想使用 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&amp;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&amp;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 is not defined":

<body>
    <script type="text/javascript">OmniFaces.DeferredScript.add('/testApp/javax.faces.resource/eNpLL81JLE7OsMoq1s8qLE0tqoRSermZeXpZxQDDagwa.js?ln=omnifaces.combined&amp;v=0');</script>
</body>

而且我注意到,当 CombinedResourceHandler 处于活动状态时,甚至 jsf.js 也不包括在内。浏览器控制台告诉 "mojarra is not defined"。

我做错了什么?提前致谢!

我的环境是:Mojarra 2.2.12,Omnifaces 2.5.1,Tomcat8.

我上周末转载了一个非常相似的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>

另请参阅:

  • Configuration of com.sun.faces.config.ConfigureListener
  • How to properly install and configure JSF libraries via Maven?

对于遇到此问题的任何其他人,我也使用 Jboss EAP 7.1 运行 解决了此问题。

我不小心在 Web 片段 JAR 的 faces-config.xml 和 Web 应用程序本身的 faces-config.xml 中声明了 OmniFacesCombinedResourceHandler。声明两次会导致与上面列出的问题相同的症状。一旦我将它从 webapp faces-config.xml 中删除,它就开始工作了。

我提出了关于 OmniFaces 问题的问题,以便在发生这种情况时可能检测并提出错误:https://github.com/omnifaces/omnifaces/issues/504