I have a doubt regarding the purpose of @PreserveOnRefresh annotation. What is the purpose of this particular annotation and what are the scenarios in which it has to be used? What are the effects of using/not using this annotation?
Thanks, Daniccan VP
The other answer is not quite correct as noted in the comments (summarized here).
This Answer applies to Vaadin 7.
Vaadin 6 is different in some ways.
As soon as a user’s browser reaches your Vaadin app, a Servlet session is created. The VaadinSession contains a wrapper around that Servlet session, reachable via VaadinSession.getSession().
The session continues until a time-out occurs, you explicitly close it, user quits the browser app, or other such terminating event. But the user hitting the Reload/Refresh feature is not such a terminating event.
While that session is on-going, the user may click/tap the Reload/Refresh icon feature in the web browser. By default this causes your current UI subclass object to be replaced by a new fresh UI subclass object.
To your user, it appears your app has restarted. But actually your app was not interrupted, only the content in that particular browser window/tab was discarded. Your app (your VaadinSession) lives on.
Your app may have had other browser windows/tabs open, each with their own UI subclass object. Those other windows/tabs would still be running with the same UI object. All of those running UI instances are tied to the same VaadinSession
.
You may or may not want your UI subclass object discarded on a Reload/Refresh of the browser window/tab, depending on the nature of your particular app.
Do you want the user to be able to do a "do over", to start again from scratch? If so, keep this default behavior. Do not apply the annotation.
If you want to change the effect of a browser Reload/Refresh to keep the UI object and its state, apply the PreserveOnRefresh
annotation. Easy, almost like magic.
In Vaadin 7, you may store state either at the level of a browser window/tab (a UI
instance) or app-wide (on VaadinSession).
If you want some data to survive the discarding of a UI
object, store that data on the VaadinSession by calling the get/setAttribute methods. For example, you would keep user login/authentication information on the VaadinSession.
To learn more these two levels of scope, including crucial information about thread-safety, see this other question, how to put data in session variable and get the data in different page in vaadin?.
This diagram shows the session hierarchy. The upper three levels (Servlet Container, ServletContext, and HttpSession) are all standard Servlet technology. Below those are the Vaadin-specific levels.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With