发送的Ajax请求未定义触发事件 [英] Ajax request sent without defining the triggered event

查看:87
本文介绍了发送的Ajax请求未定义触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个最小,完整和可验证的示例,该示例按预期工作,其中有一个我还没有完全理解的问题.可以从下面看到该代码,其行为基本上包括从PF顶化的<p:selectOneListbox>列表中选择一个项目,并在JSF <h:outputText>元素上显示该项目的值.

I've got a minimal, complete, and verifiable example working as expected, on which there is an issue I don't finish to understand. The code can be seen below and its behaviour basically consists of selecting an item from a list of a PF ajaxified <p:selectOneListbox> and displaying the item's value on a JSF <h:outputText> element.

<h:form id="myform">
    <p:selectOneListbox id="myselect" value="#{bean.optionSelected}">
        <p:ajax listener="#{bean.onChange}" process="myselect" update="toupdate" onstart="onstart()" oncomplete="oncomplete()" onerror="onerror()" onsuccess="onsuccess()"/>
        <f:selectItem itemLabel="Option 1" itemValue="1" />
        <f:selectItem itemLabel="Option 2" itemValue="2" />
        <f:selectItem itemLabel="Option 3" itemValue="3" />
    </p:selectOneListbox>    

    <h:outputText id="toupdate" value=">#{bean.optionSelected}" />
</form>

看着该元素,我不仅仅知道是什么特定事件导致ajax请求发送到服务器,也就是说,我不知道触发的事件是否是 valuechange 事件或其他事件.换句话说,我想念这样编码的<p:ajax>元素:

Looking at the element, I don't just know what specific event is causing the ajax request to be sent to the server, that is, I don't know if the triggered event was the valuechange event or some other. In other words, I miss a <p:ajax> element coded in this way:

<p:ajax event="name_of_the_event" .../>

这个疑问使我不知道支持bean端的侦听器方法将使用的接收事件的类: public void onChange(??? event)

And this doubt makes me to not know the class of the receiving event to be used by the listener method in the backing bean side: public void onChange(??? event)

任何澄清/解释将不胜感激.谢谢.

Any clarification/explanation would be really appreciated. Thanks.

推荐答案

根据PrimeFaces 将调用侦听器.

According to the PrimeFaces dropdown showcase <p:ajax listener="..." /> inside a <p:selectOneListbox /> will call the listener when the user selects a different item (onchange).

ajax标记文档表示event属性是可选的,并且:

And the ajax tag documentation says that the event attribute is optional and:

客户端事件触发ajax请求.默认值由行为附加到的父ClientBehaviorHolder组件定义.

Client side event to trigger ajax request. Default value is defined by parent ClientBehaviorHolder component the behavior is attached to.

如果要

  • 以默认事件为参数调用方法
  • 调用没有任何参数的方法

所以<p:ajax listener="#{myBean.onAjaxAction}" />

会打电话

public void onAjaxAction(){
  ...
}

如果没有其他方法具有更匹配的方法签名.

If there is no other method with a more matching method signature.

如果要获取有关事件的更多信息,可以保留EL标识,并在服务器端添加事件.每个具体事件作为参数都应扩展javax.faces.event.AjaxBehaviorEvent.

If you want to have more info about the event, you can leave the EL identival and server-side add an event. Each concrete event as a parameter should extend javax.faces.event.AjaxBehaviorEvent.

public void onAjaxAction(javax.faces.event.AjaxBehaviorEvent event) {
  System.out.println(event.getClass())
}

然后您可以在其他事件中检索事件源.如果您需要更多信息(如果有),则需要使用更具体的事件类.如果您不知道具体的默认事件(及其对应的类)是什么,则可以添加上面的方法,然后在该方法中尝试检查具体的事件(在System.out...中已完成)

You can then retrieve the event source amongst other things. If you need even more info (if available) you need to use a more concrete event class. If you do not know what the concrete default event (and its corresponding class) is, you can add the method above and in the method try to inspect the concrete event (as is done in the System.out...)

对于许多PrimeFaces组件,事件在文档中都有提及.并且可以找到所有现有的PrimeFaces事件(针对所有组件)

For many PrimeFaces components the events are mentioned in the documentation. And all existing PrimeFaces events (for all components) can be found in the org/primefaces/event package so using code-completion in an IDE would give you options (mind that not all work in all components obviously).

如果文档中没有显式命名的事件,则基本dom事件至少应起作用,包括onchange.对于输入,最常见的情况是"onchange"是默认事件.

If there are no explicitly named events in the documentation, the basic dom events should at least work, including the onchange. Most often for inputs 'onchange' is the default event.

由于<p:selectOneListbox />中没有在文档中提及显式事件,因此触发ajax请求的默认事件将是onchange.为此,如有必要,应将javax.faces.event.AjaxBehaviorEvent用作参数.

Since for the <p:selectOneListbox /> there are no explicit events mentioned in the docs, the default event to trigger ajax request will be onchange. For this the javax.faces.event.AjaxBehaviorEvent should be used as parameter if needed.

这篇关于发送的Ajax请求未定义触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆