Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Be careful to use BookmarkablePageLink in favour of PageLink. The latter is stateful by design.

Stateless pages in

...

1.

...

3 (

...

backported

...

from 2.

...

0)

 
Since 1.3/ 2.0, Wicket supports deferred session creation. So as long as you use stateless pages, there won't be any session created at all.

Stateless support was extended in 2.0 (and 1.3) to allow much more control in whether/ when a (bookmarkable) page is flagged stateless. You can now mark components and behaviors (e.g. ajax) with callbacks as being stateless by overriding Component#getStatelessHint() or IBehavior#getStatelessHint() method. By default most components and behaviors (except for ajax behaviors) have stateless hint returning true.

Component is considered stateless if it and all its behaviors hint that they are stateless. As long as all components and behaviors on a page are stateless and the page is bookmarkable (see Bookmarkable pages and links), the page is regarded stateless. Convenience classes that support this are StatelessLink and StatelessForm.You can set stateless hint for a page using Page#setStatelessHint() method. It is useful when you want to be sure that page is stateless. If this hint is set to true but the page contains some stateful components you will be warned. But unlike a component if you set this hint to false the page can still be stateless if all components on it are stateless.

The most common and not stateless components are Link and Form. To use links and forms in a stateless way there are convenience classes StatelessLink and StatelessForm. Notice that every time a stateless page is requested wicket (at least 1.3) creates new instance of that page, so in event handling code of form or link you should assume that page's member variables don't have the same values they had in the "previous" instance of that page.

A StatelessLink can currently only be something like a navigation link. Or if you generate the URL with some extra info you can get that from the RequestParameters.

Code Block

public class MyPage extends WebPage {
    private int i;

    public MyPage(PageParameters parameters) {
        add(new StatelessLink("link") {
            public void onClick() {
                System.out.println(i++); // will always print 0
                // you can use PageParameters
                setResponsePage(AnotherPage.class);
            }
        });
    }
}

Since Where the StatelessForm is almost like an a normal form, the onSubmit would will be pretty much the same as a for normal form. Except when you rely on state outside the form components itself, so such as page state
or state that isn't pushed by the post data. So if your onSubmit code just depends on the form state that got pushed by the submit of the browser, what is normally the case, then there is no change at all. A StatelessLink can currently only be something like a navigation link. Or if you generate the url with some extra info you can get that from the RequestParameters and use that.

Code Block

public class MyPage2 extends WebPage {
    private final Map<String, String> map = new HashMap<String, String>();

    public MyPage2() {
        map.put("text", null);
        map.put("anotherText", null);
        final StatelessForm form = new StatelessForm("form", new CompoundPropertyModel(map)) {
            protected void onSubmit() {
                // map here contains submitted data
                System.out.println(map.get("text"));
                System.out.println(map.get("anotherText"));
                map.put("anotherText", "this will never be printed");
            }
        };
        form.add(new TextField("text"));
        add(form);
    }
}

TODO expand, give some examples. it is ok?

See also explanation on the list