Stateless Apache Wicket stateless pages/requests

时光毁灭记忆、已成空白 提交于 2019-12-03 13:36:50

问题


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?


回答1:


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.




回答2:


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.




回答3:


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



回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/2178285/stateless-apache-wicket-stateless-pages-requests

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!