p:ajax开火两次 [英] p:ajax fires twice
问题描述
关于PrimeFaces的p:ajax.我的<p:commandButton>
中有两个p:ajax请求.我在第二种方法中做了stdout
,所以我可以查看它是否被触发.相同(最后)的方法被触发两次,我不知道为什么.如果我仅在按钮中提出一个<p:ajax>
请求,则他将触发一次;如果我提出两个<p:ajax>
请求,则将其两次触发.
concerning p:ajax of PrimeFaces. I have two p:ajax requests within my <p:commandButton>
. I did stdout
within the second method so that I can see if its triggered or not. The same (last) method is triggered twice and I don't know why. If I put only one <p:ajax>
request into my button he fires once and if I put two <p:ajax>
requests he fires twice.
<h:body style="background:#f5f5f5;">
<h:form id="formG">
<p:commandButton styleClass="viewButton" icon="ui-icon-search"
value="#{msg['button.open']}" id="auftragButtonG">
<p:ajax listener="#{auftragBean.saveIdIntoAppScope()}" partialSubmit="true"/>
<p:ajax listener="#{auftragBean.loadXMLData()}" partialSubmit="true"/>
</p:commandButton>
</h:form>
</h:body>
public void loadXMLData(){
setXmlData(entireXmlData(mandantId(), getJobId()));
System.out.println(getXmlData().size());
}
任何人都知道为什么以及如何忽略第二个负担吗?
Anybody knows why and how I can ignore the second load?
推荐答案
这是因为您正在指示组件在同一(默认)事件上触发两个Ajax请求.一个<p:ajax>
视为一个请求.换句话说,代码的行为与您编写的代码完全相同.
It's because you're instructing the component to fire two ajax requests on the same (default) event. One <p:ajax>
counts as one request. In other words, the code is behaving exactly as you programmed it.
您需要以不同的方式解决您的具体问题.
You need to solve your concrete problem differently.
-
调用一种方法,该方法又委派给所需的方法.
Invoke one method which in turn delegates to the desired methods.
<p:commandButton ... action="#{bean.methodWhichInvokesBothMethods()}" partialSubmit="true" />
在一个调用中调用多个方法.您可以使用<f:actionListener binding>
技巧.
<p:commandButton ... partialSubmit="true">
<f:actionListener binding="#{bean.method1()}" />
<f:actionListener binding="#{bean.method2()}" />
</p:commandButton>
无论哪种方式,<p:ajax>
都是完全不必要的,因为<p:commandButton>
本身已经具有内置的ajax支持.
Either way, the <p:ajax>
is totally unnecessary as the <p:commandButton>
by itself already has builtin ajax support.
这篇关于p:ajax开火两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!