开发者

Stateless Apache Wicket stateless pages/requests

开发者 https://www.devze.com 2022-12-18 23:33 出处:网络
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.

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?


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.


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.


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);
        }
    }
}


If you have pages you intentionally want to make sure are stateless the setStatelessHint(boolean state) method is useful.

It gives off a warning if the page isn't stateless.

for more information see here: Wicket Stateless pages


What about the situation where the page can be stateless or stateful depending on whether or not the user has authenitcated?

An example could be the typical 'account' panel that resides at the top of most web pages that shows the currently logged on username, profile link etc.,

Most pages on the site would have this at the top so that means both pages must be able to be both stateful and stateless depending on whether a user has logged on.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号