在JSF 2.0中如何处理AJAX请求? [英] How is AJAX request handled in JSF 2.0?
问题描述
我正在尝试类似以下的内容-
I was trying something like below --
<h:commandButton id="btnOK">
<f:ajax event="action" execute="@this" render="idHeader"
listener="#{myBean.actionSubmit()}"/>
</h:commandButton>
我想当该ajax请求完成时,在UI中进行一些处理.为此,我正在使用以下jQuery代码:
And I want when that ajax request completes, do some processing in the UI.I am using following jQuery code for that :
$(document).ajaxComplete(function() {
$.modal.close();
});
但是我的问题是jQuery方法根本没有被触发.我想知道JSF ajax提交是否触发ajaxComplete事件?
But my problem is the jQuery method is not fired at all. I am wondering if JSF ajax submission fires the ajaxComplete event or not??
推荐答案
标准的JSF impl不使用jQuery API发送ajax请求. JSF使用自己的内部API,您可以在自动包含的jsf.js
文件中找到它.因此,永远不会调用jQuery jQuery.ajaxComplete()
.仅在使用jQuery的$.ajax()
(及其快捷方式$.get()
,$.post()
等)时才调用它.只有基于jQuery的组件库才能使用它们,例如带有<p:commandXxx>
和<p:ajax>
的PrimeFaces.对于标准JSF <f:ajax>
,您需要使用JSF自己的jsf.ajax.addOnEvent()
处理程序,或者使用<f:ajax>
的onevent
属性.
The standard JSF impl doesn't use jQuery API to send ajax requests. JSF uses its own internal API which you can find in the auto-included jsf.js
file. So the jQuery jQuery.ajaxComplete()
is never called. It would only be called when jQuery's $.ajax()
(and its shortcuts $.get()
, $.post()
, etc) is being used. Only jQuery based component libraries use them such as PrimeFaces with <p:commandXxx>
and <p:ajax>
. For standard JSF <f:ajax>
, you need to use JSF's own jsf.ajax.addOnEvent()
handler instead, or the onevent
attribute of the <f:ajax>
.
在您的特殊情况下,我认为onevent
属性很简单:
In your particular case I think the onevent
attribute is mose easy:
<f:ajax ... onevent="btnOKhandler" />
使用
function btnOKhandler(data) {
if (data.status == 'success') {
$.modal.close();
}
}
data.status
可以具有3个值:begin
,complete
和success
.另请参见 JSF规范的表14-4和14-3.
The data.status
can have 3 values: begin
, complete
and success
. See also tables 14-4 and 14-3 of the JSF specification.
这篇关于在JSF 2.0中如何处理AJAX请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!