有一种简单的方法可以在调用之前和之后调用JavaScript动作<f:ajax listener>,例如,我想在后备bean中调用window.alert("pre") 之前 和window.alert("post") 之后 调用JavaScript动作:onChange``ACtrl
<f:ajax listener>
window.alert("pre")
window.alert("post")
onChange``ACtrl
<h:form> <h:inputText id="anId" value="#{cityCtrl.dbHost}"> <f:ajax event="change" listener="#{aCtrl.onChange}" execute="@all"/> </h:inputText> </h:form> @ManagedBean public class ACtrlimplements Serializable { public void onChange(AjaxBehaviorEvent event) { System.out.println("something changed"); } }
添加多个f:ajax元素似乎不起作用(也许应该吗?!),例如
f:ajax
<h:form> <h:inputText id="anId" value="#{cityCtrl.dbHost}"> <f:ajax event="change" listener="#{aCtrl.toggle}" execute="@all"/> <f:ajax event="change" listener="#{aCtrl.onChange}" execute="@all"/> <f:ajax event="change" listener="#{aCtrl.toggle}" execute="@all"/> </h:inputText> </h:form> @ManagedBean public class ACtrlimplements Serializable { public void onChange(AjaxBehaviorEvent event) { System.out.println("something changed"); } public void toggle(AjaxBehaviorEvent event) { System.out.println("blah"); } }
仅ACtrl.onChange被调用。
ACtrl.onChange
使用onevent属性。它必须指向一个回调函数引用(所以不要包括括号!):
onevent
<f:ajax ... onevent="functionName" />
实际的回调函数如下所示(JSF会自己提供参数):
function functionName(data) { var status = data.status; // Can be "begin", "complete" or "success". var source = data.source; // The parent HTML DOM element. switch (status) { case "begin": // Before the ajax request is sent. // ... break; case "complete": // After the ajax response is arrived. // ... break; case "success": // After update of HTML DOM based on ajax response. // ... break; } }