Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stateless Apache Wicket stateless pages/requests

Tags:

java

wicket

So I was reading another question under the Wicket tag comparing Apache Wicket and Apache Click. A concise explanation, in my opinion. I am more familiar with the Wicket world than the Click world.

One of the comments I read was that you can make stateless Wicket pages. I started to think about this and couldn't figure out a way to make a request or a page request for something stateless. This could certainly come in handy in some situations. So how does one start to use Wicket without state?

like image 432
Matt Avatar asked Feb 01 '10 16:02

Matt


3 Answers

Wicket is stateless by default, and switches into stateful mode when it needs to. It is very easy to break the stateless mode.

I've found it useful to annotate intended stateless pages and stateless components with @StatelessComponent, which is found in the wicket-devutils project. I then add a StatelessChecker in my WebApplication.init() method like this:

protected void init(){
    ...
    this.addPostComponentOnBeforeRenderListener(new StatelessChecker());
    ...
}

This way I always get an exception about the offending stateful component.

like image 114
Norbert Madarász Avatar answered Nov 15 '22 06:11

Norbert Madarász


If a page is bookmarkable and doesn't contain any non-stateless components/behaviors then the page is automatically stateless and not stored in the session. I think that as long as a user visits only stateless pages, a session will not be created. For the most part, if everything about how the page is displayed can be determined solely from a no-args constructor or a constructor taking a PageParameters argument. The normal Link and Form classes are not stateless, so you'll need to use StatelessForm and StatelessLink instead.

like image 43
Geoff Reedy Avatar answered Nov 15 '22 08:11

Geoff Reedy


I prefer to check that in test.

so each test for stateless page overrides

getStatelessWebPage()

which by default returns null.

then in basic test I have generic test that visits all components on page and check whether component is stateless or not

@Test
public void checkForStateless()
{
    StatelessWebPage statelessPage = getStatelessWebPage();
    if (statelessPage != null)
    {
        Page page = (Page)statelessPage;
        if (!page.isPageStateless())
        {
            //find the reason
            Component statefulComponent = page.visitChildren(Component.class, new StatelessChecker());
            if (statefulComponent != null)
            {
                fail("Stateless page contains stateful component ["
                     +statefulComponent.getClass().getName()+" : "
                     + statefulComponent.getMarkupId() +"]");
            }
        }
    }
}

and

class StatelessChecker implements IVisitor<Component, Component>
{
    @Override
    public void component(Component component, IVisit<Component> iVisit)
    {
        if (!component.isStateless())
        {
            iVisit.stop(component);
        }
    }
}
like image 38
kamiseq Avatar answered Nov 15 '22 08:11

kamiseq