Stateless Apache Wicket stateless pages/requests

后端 未结 5 1526
感情败类
感情败类 2021-02-06 13:53

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 t

相关标签:
5条回答
  • 2021-02-06 14:18

    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.

    0 讨论(0)
  • 2021-02-06 14:21

    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.

    0 讨论(0)
  • 2021-02-06 14:21

    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

    0 讨论(0)
  • 2021-02-06 14:30

    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);
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-06 14:35

    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 讨论(0)
提交回复
热议问题