2014-12-29 22 views
2

這是我的複合材料構件hc:rangeChooser不能調用監聽方法從F:阿賈克斯複合組件內部

<ui:component xmlns="http://www.w3.org/1999/xhtml" 
       xmlns:f="http://java.sun.com/jsf/core" 
       xmlns:h="http://java.sun.com/jsf/html" 
       xmlns:ui="http://java.sun.com/jsf/facelets" 
       xmlns:cc="http://java.sun.com/jsf/composite" 
       xmlns:p="http://primefaces.org/ui"> 
    <cc:interface componentType="rangeChooser"> 
     <cc:clientBehavior name="rangeSelected" event="change" targets="#{cc.clientId}:datetype"/> 
    </cc:interface> 

    <cc:implementation> 

     <div id="#{cc.clientId}"> 
      <h:outputScript library="primefaces" name="jquery/jquery.js" target="head" /> 
      <h:outputScript library="primefaces" name="jquery/jquery-plugins.js" target="head" /> 
      <h:outputScript library="javascript" name="rangeChooser.js" /> 
      <h:outputStylesheet library="primefaces" name="primefaces.css" target="head" /> 
      <h:outputStylesheet library="primefaces" name="jquery/ui/jquery-ui.css" target="head"/> 

      <script type="text/javascript"> 
       var variables = {}; 
       variables.formid = '#{cc.clientId}'; 
      </script> 

      <h:selectOneMenu id="datetype" value="#{cc.attrs.selectedRange}" onchange="dateFunc();"> 
       <f:selectItems value="#{cc.attrs.ranges}"></f:selectItems> 
      </h:selectOneMenu> 

      .... 

      <p:inputText id="hiddenValue" value="#{cc.attrs.range}"/> 
     </div> 
    </cc:implementation> 
</ui:component> 

當我在<h:selectOneMenu>選擇一些東西我跑dateFunc();這通選擇的值從它<h:inputText>,在未來它會被隱藏。現在

,這是一個辦法,我如何使用這個組件:

<h:form id="form"> 
    <p:growl id="growl"></p:growl> 
    <hc:rangeChooser1> 
     <f:ajax event="rangeSelected" 
       onevent="alert('It works');" 
       listener="#{testBean.update}" 
       update=":form:growl"/> 
    </hc:rangeChooser1> 
</h:form> 

最後我支持bean的方法:

public void update(AjaxBehaviorEvent e){ 
    SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy"); 
    text = df.format(from.getTime()) + " " + df.format(to.getTime()); 
    FacesContext context = FacesContext.getCurrentInstance(); 

    context.addMessage(null, new FacesMessage("Successful" + text, "Your message: " + text)); 
    context.addMessage(null, new FacesMessage("Second Message", "Additional Message Detail")); 
} 

當我改變選擇的一切工作正常,除了<f:ajax>沒有按不要致電testBean更新method,但它調用函數alert('it works');

當我使用<p:remoteCommand>我可以從<f:ajax onevent="myRemote">調用,然後定義

<p:remoteCommand name="myRemote" actionListener="#{testBean.update}" 
       update="growl"/> 

但我不喜歡這樣的解決方案,不明白爲什麼我的做法是行不通的。

回答

3

首先,

<cc:clientBehavior ... targets="#{cc.clientId}:datetype" /> 

targets屬性的值是錯誤的。它的解釋是相對於<cc:implementation>,但#{cc.clientId}錯誤地認爲它是相對於其父母的NamingContainer解釋。擺脫#{cc.clientId}:前綴。

<cc:clientBehavior ... targets="datetype" /> 

不要忘記通知源的作者,你發現這個錯誤信息。這不是我第一次看到首發使用這個,所以互聯網上一定有一些消息來源傳播這種錯誤信息,並且應該被告知。

其次,

<f:ajax ... update=":form:growl" /> 

update屬性不支持<f:ajax>。這是PrimeFaces特定的。你可能打算使用render

<f:ajax ... render=":form:growl" /> 

無關到具體的問題:<f:ajax onevent="alert('It works');">可以工作,不可能作爲這個結構不被支持。 onevent屬性的值應該表示函數引用的名稱,而不是函數調用。因此,無論是觀測還是過度簡化/假設太多而沒有實際測試。

+0

非常感謝@BalusC,本文給出了這個例子:http://www.ibm.com/developerworks/java/library/j-jsf2fu-0610/index.html – Anatoly

+0

哦。這是一篇相當古老的文章。它很可能依靠史前錯誤。至少不是正確的方法。我會留下作者一封郵件,謝謝。 – BalusC

+1

再次感謝您的工作,並藉此機會,我想感謝您的寶貴文章和答案。 – Anatoly