无法从复合组件中的f:ajax调用侦听器方法

时间:2014-12-29 11:52:51

标签: jsf jsf-2 composite-component

这是我的复合组件hc:rangeChooser

<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> ,以后它将被隐藏。

现在,这是我使用此组件的方式:

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"));
}

最后我的支持bean的方法:

<f:ajax>

当我更改选择时,一切正常,但method不会调用testBean的更新<p:remoteCommand>,但它会调用函数警报('it works');

当我使用<f:ajax onevent="myRemote">时,我可以从<p:remoteCommand name="myRemote" actionListener="#{testBean.update}" update="growl"/> 拨打电话然后定义

{{1}}

但是我不喜欢这个解决方案而且不明白为什么我的方法不起作用。

1 个答案:

答案 0 :(得分: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属性的值应表示函数引用的名称,而不是函数调用。所以无论是误操作还是过度简化/假设太多而没有实际测试它。