0

In my application, I am adding form data in database on click of submit button.But the problem is, when user refreshes the page, the data added one more time into the database and so on...

What can be the solution for this, please some body give me good link/solution for this problem...

Thanks a lot.

Code :

<h:commandButton value="Submit" action="#{co.createAction()}" />

public String createAction() {
  if (login.registerUser().equals("success")) {
    return "index?faces-redirect=true";
  } else {
    return "failure";
  }
}

public String registerUser() {
  if (serv.createRegUser(reg)) {
    return "success";
  } else {
    return "failed";
  }
}

public boolean createRegUser(Registration reg) { 
  try { 
    initJpa(); 
    regJpa.create(reg); 
    return true; 
  } catch(Exception e) { 
    e.printStackTrace(); 
    return false; 
  } 
}
Thrax
  • 1,926
  • 1
  • 17
  • 32
user3231655
  • 75
  • 2
  • 11
  • Probably something is wrong and as you observed you add data to database on refresh. For a more detailed diagnosis give us more relevant details like code etc. – Deltharis Oct 24 '14 at 11:01
  • Here is my code public String createAction(){ if(login.registerUser().equals("success")){ return "index?faces-redirect=true"; }else{ return "failure"; }}public String registerUser(){ if(serv.createRegUser(reg)){ return "success"; }else{ return "failed"; } } ` public boolean createRegUser(Registration reg) { try { initJpa(); regJpa.create(reg); return true; } catch(Exception e) { e.printStackTrace(); return false; } }` – user3231655 Oct 24 '14 at 13:20
  • @user3231655 so even after 10 minutes have passed, it still doesn't cross you mind to edit your question and add the code there rather than making it unreadable in a comment? – Gimby Oct 24 '14 at 13:33

2 Answers2

0

Also avoid using void methods in JSF. If you want to return back to same page use the same page viewid. Try to put the following code:

public String submit() {
    // ...

    return "viewid?faces-redirect=true";
}

The reference is taken from this link.

Community
  • 1
  • 1
Vighanesh Gursale
  • 921
  • 5
  • 15
  • 31
0

This could be caused by several factors :

1 - Your function adding the item in the database is called each time the view is displayed.

This could be caused by a @PostConstruct annotation on the aforesaid method. This could also be caused by an element calling this method from the view.

2 - Your method for inserting object in database is persisting 2 objects

3 - Your form is submitted more than once

4 - You inserted a second object if the DB when you observed

Thrax
  • 1,926
  • 1
  • 17
  • 32
  • Here is my code public String createAction(){ if(login.registerUser().equals("success")){ return "index?faces-redirect=true"; }else{ return "failure"; }}public String registerUser(){ if(serv.createRegUser(reg)){ return "success"; }else{ return "failed"; } } ` public boolean createRegUser(Registration reg) { try { initJpa(); regJpa.create(reg); return true; } catch(Exception e) { e.printStackTrace(); return false; } }` – user3231655 Oct 24 '14 at 13:20