6 Replies Latest reply on Feb 22, 2008 4:21 PM by Kenneth Christensen

    Event scope not behaving as expected

    Gavin Ellis Newbie

      I'm fairly new to seam so just playing around with the various seam scopes to see if they behave as expected. I've created a basic action and bean, with corresponding @Local interface (not shown), as below:


      
      @Name ("listMenuAction")
      
      @Stateful
      
      public class ListMenuAction implements ListMenu{
      
      
           @In @Out
      
           Menu menu;
      
      
               @public void processAction(){
      
                   System.out.println(menu.getName());
      
                   menu.setName("myMenu");
      
               }
      
               
      
               @Remove
      
               public void destroy(){}
      
      
      }
      
      
      @Name ("menu")
      
      @Scope (ScopeType.EVENT)
      
      public class Menu {
      
      
          Integer id;
      
          String name;
      
          Collection<Menu> items;
      
      
          //...
      
      }
      
      



      and a form which calls this action. The relevant part being:


      
      <h:form id="menuForm">
      
        <h:inputText id="id" value="#{menu.id}"/>
      
        <h:inputText id="name" value="#{menu.name}"/>
      
        <h:commandButton id="menuForm" action="#{listMenuAction.processAction}" value="submit"/>
      
      </h:form>
      
      



      This form simply posts and redirects to itself.


      If I set the scope type to be SESSION or CONVERSATION, then the menu is injected/outjected correctly from/to the form and the name can be printed out in the action and rendered in the facelet.


      If, however, I set the scope type to EVENT or PAGE then the correct values are read in from the form, but are not then available to the facelet in the render response phase.


      As I understand it a scope type of EVENT should last until the render response phase. Is this the case, or am I having a moment? ;)

        • 1. Re: Event scope not behaving as expected
          Gavin Ellis Newbie

          Please ignore the @public typo...

          • 2. Re: Event scope not behaving as expected
            Kenneth Christensen Novice

            As I understand it a scope type of EVENT should last until the render response phase.

            Yes, if you are not using redirects. A redirect triggers a new request (event).

            • 3. Re: Event scope not behaving as expected
              Gavin Ellis Newbie

              Thanks for this swift response. That makes sense in as much as I was using a redirect to return to this form. I know this happens automatically if you are returning to the same page, this was just a simulation of navigating to another page. I am a little confused now, however. The definition in JavaDocs for ScopeType.EVENT is



              The event (request) context. Spans a server request, from restore view to render response.

              so by this definition a redirect constitutes a new server request. In the case where I do want to redirect to a new page, but also want to make some outjected beans available to the new view, could you tell me which seam context I should use to do this. The first scope that seems to do what I'm after is the session scope, but this is a wider scope than I require for this case. Is there some other mechanism that can be used to handle this case?

              • 4. Re: Event scope not behaving as expected
                Kenneth Christensen Novice

                Use a conversation (temp. or long-running - depends on your use-case/requirements) scope - stateful session beans and entities are default conversation scoped.

                • 5. Re: Event scope not behaving as expected
                  Gavin Ellis Newbie

                  Specifying a render in the navigation rule (with the component in the EVENT context) rather than a redirect seems to be sufficient for the simple case of navigating from page A to page B e.g.


                  <page view-id="/listMenu.xhtml">
                           <navigation from-action="#{listMenuAction.processAction}">
                                <render view-id="/listMenu.xhtml"></render>             
                           </navigation>       
                  </page>
                  



                  rather than


                  <page view-id="/listMenu.xhtml">
                           <navigation from-action="#{listMenuAction.processAction}">
                                <redirect view-id="/listMenu.xhtml"></render>             
                           </navigation>       
                  </page>
                  

                  • 6. Re: Event scope not behaving as expected
                    Kenneth Christensen Novice

                    Correctly. No new request (event) is created if you're using a 'render' - again it all depends of your use-case/requirements if you want to use a 'render' or a 'redirect'.