6 Replies Latest reply on Aug 24, 2007 4:38 AM by Francis LIMOUSY

    Difference between Page and Event scope

    Francis LIMOUSY Newbie

      Hello,

      I still don't get how page scope is used.
      Doc. says "You can have access to those components from all events emitted from this page".
      Does that mean that this scopes remains till I don't move to another page ?

      I remembered using <t:updateActionListener> from Tomahawk some time ago; it serialized the value given, allowing it to persist other multiple re-submits of a page (when an input was incorrect for example).

      Does the Page scope do the same ?

        • 1. Re: Difference between Page and Event scope
          Andy Gibson Novice

          *Bump*

          Yeah, I'm wondering the same thing. I thought it did as you are suggesting, but when I tested that assumption back around 1.2.0, it didn't quite work out (can't remember why).

          A Page scope such as this would be great for search pages where you want the scope to last as long as you are on the page (with paginated search results, and/or datamodel selections), and then disappear when you click out of it. Otherwise you have to start a conversation, and make sure it ends when you leave the page. Seems odd to have to start a conversation for a single page, although technically it is correct since the user is having a conversation with the single page over multiple submits.

          • 2. Re: Difference between Page and Event scope
            Dustin Norlander Apprentice

            Here is how I would explain it, if I get anything wrong anyone is welcome to correct me.

            Event scope is essentially stateless, the bean is created for a single action and destroyed when that action is complete.

            Page scope maintains its state until the page is rendered. So here is an example that might illustrate it better:

            you have a bean with a couple of properties:

            @Scope(PAGE)
            @Name("demoBean")
            ....
            int id;
            String message = "message not set";
            
            public int getId() ...
            public void setId(int id) {
             this.id = id;
             this.message = "ID Is set!";
            }
            
            public String getMessage()
            public void setMessage(String message)
            ....
            


            Say you set your id as a page param defined in pages.xml. Then you could successfully access #{demoBean.message} within your page and get the expected result ("ID Is set!"). If you set the scope to event then you would get ("message not set").

            Hopefully that helps,
            Dustin


            • 3. Re: Difference between Page and Event scope
              Dave Whittaker Novice

              I think the most simple explanation is that event scope means that you're data is stored in the request (servlet or portlet). It is available through the request - response lifecycle. Page scoped data is stored in the faces tree so if you were to create a page scoped object while a page was being rendered, it would survive the postback to the server and be available through at least the following invoke application phase. The thing to keep in mind with page scoped data, however, is that because it's stored in the component tree it will usually be serialized between the time the page is rendered and the postback. For attached entity beans that is usually not a good thing.

              • 4. Re: Difference between Page and Event scope
                Andy Gibson Novice

                Thanks both of you for taking a stab at it. So basically, it is an object that has the same kind of duration as the JSF component tree while an event duration lasts until the end of the request.

                • 5. Re: Difference between Page and Event scope
                  Dave Whittaker Novice

                  You got it. As far as what you were saying before.... you should be able to have a page scoped attribute remain set indefinitely as long as your action method returns null, or match some other type of navigation rule that causes the page to be re-rendered. I've never tried it, I'm not a big fan of page scope because of the serialization, but I think it should work.

                  • 6. Re: Difference between Page and Event scope
                    Francis LIMOUSY Newbie

                    Ok now I got it. Thanks a lot !

                    In fact it's very useful but in a small number of cases :)