這是我的複合材料構件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"/>
但我不喜歡這樣的解決方案,不明白爲什麼我的做法是行不通的。
非常感謝@BalusC,本文給出了這個例子:http://www.ibm.com/developerworks/java/library/j-jsf2fu-0610/index.html – Anatoly
哦。這是一篇相當古老的文章。它很可能依靠史前錯誤。至少不是正確的方法。我會留下作者一封郵件,謝謝。 – BalusC
再次感謝您的工作,並藉此機會,我想感謝您的寶貴文章和答案。 – Anatoly