1

I have JSF project. I have JPA entities to persist the data. In frontend the user can edit the detached entities and send the date to backend by clicking a save button.

But it is possible that the user forgets to save the data and click on another page in the project. In this case the application should warn the user that the user loose the unsaved data.

Is it possible to check that the detached entity is changed or not??

Kayser
  • 6,544
  • 19
  • 53
  • 86
  • 3
    Don't mix JPA concepts (detached) with JSF concepts (managed beans) and javascript functionality – SJuan76 Nov 20 '12 at 12:30
  • @SJuan76 What do you mean exactly? I have JSF and JPA in my project. And My problem is to check if the data in detached entity is changed on a JSF Page – Kayser Nov 20 '12 at 12:35
  • 1
    I think what @SJuan76 meant is that there is no need to go all the way down the stack to the DB to check if the user has changed a string that is displayed in the browser - it can be checked inside the browser. – kostja Nov 20 '12 at 12:46
  • @kostja Exactly that, to ensure that the new and old field hold the same value you do not need to know from where the original value came. – SJuan76 Nov 20 '12 at 13:19
  • 1
    Related/dupe: http://stackoverflow.com/questions/8259090/how-to-detect-unsaved-data-in-form-when-user-leaves-the-page – BalusC Nov 20 '12 at 13:39

1 Answers1

3

Since you want the page to control this, you should control this with javascript which is the only framework that controls when the browser wants to unload the page.

Define the body.onunload handler, and put the data from your JSF bean twice; once in the edition fields and other in hidden fields. In the onunload, compare hidden and edition fields. If you are using ajax, in the save button oncomplete (or with the render property, update the hidden fields with the new values).

SJuan76
  • 24,532
  • 6
  • 47
  • 87