Nicolas Frankel is an IT consultant with 10 years experience in Java / JEE environments. He likes his job so much he writes technical articles on his blog and reviews technical books in his spare time. He also tries to find other geeks like him in universities, as a part-time lecturer. Nicolas is a DZone MVB and is not an employee of DZone and has posted 228 posts at DZone. You can read more from them at their website. View Full User Profile

Preserving State Between Refreshes

03.20.2012
| 2279 views |
  • submit to reddit

Last week, we had a look at the new Vaadin 7 Root that takes the place of the old version 6 Window. Though this evolution cleans window management in version 7, it brings a change that can have important consequences for those unaware of it.

In Vaadin 7, state is not kept between refreshes, whereas in v6, it was. Anyway, this is only the default behavior, and the Vaadin team provides us with the mean to do as we please. The only thing to do is to get a handle on the Application instance behind the root and call the preserveRoot(boolean) method, like so:

public class Vaadin7Root extends Root {
 
    public void init(WrappedRequest wrapped) {
 
        getApplication().setRootPreserved(true);
    }
}

You can find here the sources of a little example application that let us play with this behavior. Let's try some things:

  1. Call the URL with a named fragment beneath it (like #test). The page displays it right of the URI fragment label.
  2. Change the fragment in the adress bar (like #testchanged) and refresh the page. The page should display the new URI fragment in the page.
  3. Now, check the Preserve root checkbox. Change the fragment for the second time (like #testsecond) and behold, the page still displays the "testchanged" value.

Whatever you want the behavior to be, Vaadin provides it :-)

Published at DZone with permission of Nicolas Frankel, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)