Primefaces commandButton calls action method on page refresh
19,610
The construct was lacking Ajax-support due to a missing head definition as it seems. In this case I just added <h:head/>
right above the <h:body>
-tag and everything worked fine.
Thanks to all contributors!
Author by
Lester
Updated on November 23, 2022Comments
-
Lester over 1 year
Completely edited: Maybe I was mixing problems and misinterpreted. After simplifying my code the question simplifies to: How can I prevent the
<p:commandButton>
from executing it's action method on page refresh (like when you hit F5 inside browser window)?JSF Code:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui"> <h:body> <h:form> <h:outputText value="#{bugBean.number}" /> <h:outputText value="#{bugBean.isComplete()}" /> <p:commandButton id="entryCommand" value="add" action="#{bugBean.increase()}" update="@form" oncomplete="#{bugBean.complete()}"/> </h:form> </h:body> </html>
backing bean code:
package huhu.main.managebean; import java.io.Serializable; import javax.enterprise.context.SessionScoped; import javax.inject.Named; @Named @SessionScoped public class BugBean implements Serializable { private static final long serialVersionUID = 1L; private int number; private boolean isComplete = false; public void increase(){ number++; } public void complete(){ isComplete = true; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public boolean isComplete() { return isComplete; } public void setComplete(boolean isComplete) { this.isComplete = isComplete; } }
Update: Even if I remove the
oncomplete
stuff like this an click the<p:commandButton>
just once, the counter goes up on every page refresh.<h:form> <h:outputText value="#{bugBean.number}" /> <p:commandButton id="entryCommand" value="add" action="#{bugBean.increase()}" update="@form"/> </h:form>
-
BalusC over 11 yearsWith "page refresh", do you mean as in pressing F5/Ctrl+R in webbrowser?
-
Lester over 11 years@BalusC Yeah, that's what I meant!
-
kolossus over 11 yearsPost the rest of your page here, to give some context.
-
Catfish over 11 yearsI'm not understanding your question. A commandButton doesn't execute when a page is refreshed, it executes when the button is pressed. Can you clarify?
-
Lester over 11 yearsThat's why theres a problem. The method IS called on refresh. Again and again.
-
BalusC over 11 yearsNo-repro on Chrome 22 / FF 16 / IE 9. Which browser exactly are you using? Are you refreshing the initial GET request or the ajax POST request?
-
Lester over 11 yearsI am using Chrome 22. I just click the button once, the I refresh the page with F5.
-
Lester over 11 yearssame with FF 16, but that one warns me that any action, that was performed, could be performed again. yeaaaaaah, did chrome warn me too? yes it did. so i suppose that is the answer...
-
Lester over 11 yearsbut isn't there a way to prevent that double form submission on refresh without touching the opportunity to click the button further times on purpose?
-
BalusC over 11 yearsSounds like that the request wasn't performed by ajax (asynchronously), but just regularly (synchronously). That can happen if you have
ajax="false"
in your real command button code (which isn't present in the code posted so far). Didn't you oversimplify the code in the question too much without testing? -
Lester over 11 yearsActually this one time I really took your advice to heart, to create a new xhtml-file and a new backing bean to be able to show the complete code. And that is the case here. Only modification is the
<h:form>
(see Update) which I cleared from alloncomplete
-stuff after dropping this issue. What you see is really my full code. And you are right the request seems to be performed regularly as my browser's (Chrome 22) refresh-button turns for a snippet of second into an stop-button which indicates something is loaded. I compared it with other ajax requests - don't show that behaviour.
-
-
Lester over 11 yearsSo the
oncomplete
method is executed on every page refresh whether the button was ever clicked or not? What about theaction
method? Even if I removeoncomplete
theaction
method is executed on page refresh even if I never ever click the button. -
perissf over 11 yearsYes, in your case the oncomplete method is executed whether the button was clicked or not, because it's an EL expression. No, the action method is not executed on each page refresh. Please check again
-
BalusC over 11 yearsLester, the
oncomplete
is supposed to execute some JS code when the action method completes. It isn't supposed to execute a JSF backing bean method at all. Any EL expression inonXXX
attribute is evaluated immediately as the page renders, simply because it needs to be converted to JS code. But this is further not related to your strange refresh problem. -
Lester over 11 years@BalusC Thanks for your answer, I understand. That's indeed good to know. I am really wondering about that refresh problem: So that is definitely a strange one, considering my code. Can anyone reproduce it with my code or has any other approach of explanation?