Nick
Nick

Reputation: 575

JSF 2.2 Form Submit, reloading page = resubmit?

So i have a JSF Application. If i submit a form, it gets resubmitted when i refresh the page. and of course i don't want that.

Index.xhtml:

[...]
    <h:form >
        <h:outputText value="Form"/><br/>
        <h:inputText label="first" value="#{example.newExample.firstWord}" autocomplete="off"></h:inputText>
         <h:commandButton value="click" actionListener="#{example.NewExample()}">        

         </h:commandButton>

    </h:form>
[...]

The method that gets runned:

  public void NewExample(){
    pfDB.InsertNewExample(NewExample);
    NewExample.setFirstWord(null);
    NewExample.setSecondWord(null);
}

So everytime i refresh index.xhtml after i submitted the form, NewExample() get runned.

Upvotes: 1

Views: 3406

Answers (2)

grigouille
grigouille

Reputation: 705

Try submitting via ajax :

<h:commandButton value="click" actionListener="#{example.NewExample()}">
  <f:ajax />
</h:commandButton>

You may need to render some components.

Upvotes: 0

Nick
Nick

Reputation: 575

It was indeed the resubmitting off the form that was the problem. But the browser(opera) didn't showed an alert.

I fixed it by adding

  FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");

in my function

Upvotes: 3

Related Questions