11 Replies Latest reply on Dec 14, 2007 10:10 AM by andygibson

    How do I reload a Data Model on a SessionBean everytime it i

      Bean...

      
      @Name("usermanager")
      @Stateful
      @Scope(SESSION)
      @Restrict("#{identity.loggedIn}")
      public class UserManager implements UserManagerInterface {
      
       @DataModel("users")
       private List<User> users;
      
       @Factory("users")
       public List<User> getUsers() { }
      
      }
      
      


      Page...

      
      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
       xmlns:s="http://jboss.com/products/seam/taglib"
       xmlns:ui="http://java.sun.com/jsf/facelets"
       xmlns:f="http://java.sun.com/jsf/core"
       xmlns:h="http://java.sun.com/jsf/html"
       xmlns:a4j="http://richfaces.org/a4j"
       xmlns:rich="http://richfaces.org/rich"
       template="layout/template.xhtml">
      
       <ui:define name="body">
      
       <h:form>
      
       <rich:dataTable id="tblUsers" var="varUser" value="#{users}">
      
       <f:facet name="header">
       <rich:column><h:outputText value="Name" /></rich:column>
       </f:facet>
       <rich:column><h:outputText value="#{varUser.name}" /></rich:column>
      
       </rich:dataTable>
      
       </h:form>
      
       </ui:define>
      
      </ui:composition>
      


      I want the datatable to be reloaded every time the page is rendered....



        • 1. Re: How do I reload a Data Model on a SessionBean everytime

          anyone?

          I tried @DataModel(scope=ScopeType.PAGE) and it still doesnt seem to be working...

          Am I doing something wrong?

          This seems pretty straight forward...

          Reload the Data Model with every time the page is rendered...

          • 2. Re: How do I reload a Data Model on a SessionBean everytime
            andygibson

            What about setting the scope on the factory to page?

            Alternatively, you could put an action in pages.xml to remove users from the contexts.

            <page view-id="/users.xhtml" action="#{userManager.invalidateUsers}">
            
            





            • 3. Re: How do I reload a Data Model on a SessionBean everytime

              if I try scope=ScopeType.PAGE on the Factory, I get some long error about injected the "user" property. I did some research in the Forums and someone said to never do scope on the Factory annotation...

              So, does the ScopeType.PAGE work for DataModel. I am assuming not...

              Well, it would be cool if there was some @Reload annotation that simply reloaded the Data Model...

              I fixed my problem by having an empty attribute that I request with a OuputText that returns and empty string and simple calls getUsers, which reloads the data model, each time the page is rendered.

              Its a pretty ugly solution, but it works.....

              Thanks

              indy

              • 4. Re: How do I reload a Data Model on a SessionBean everytime
                pmuir

                Outject the datamodel into event scope. Easy :)

                • 5. Re: How do I reload a Data Model on a SessionBean everytime

                  ok...i need some help with your suggestion pete...

                  Here is how I am Outjecting the datamodel into even scope...

                  
                  @Name("usermanager")
                  @Stateful
                  @Scope(SESSION)
                  @Restrict("#{identity.loggedIn}")
                  public class UserManager implements UserManagerInterface {
                  
                   @Out(scope=ScopeType.EVENT)
                   @DataModel("users")
                   private List<User> users;
                  
                   @Factory("users")
                   public List<User> getUsers() { }
                  
                  }
                  
                  


                  I am doing something wrong....

                  I know this is easy, I am just not getting it....

                  Thanks

                  indy

                  • 6. Re: How do I reload a Data Model on a SessionBean everytime
                    pmuir

                    That doesn't outject the datamodel into event scope. You would need an event scoped manager component which does the outjection.

                    • 7. Re: How do I reload a Data Model on a SessionBean everytime
                      mteichmann

                      Pete,

                      I am stuck with a similar problem at the moment but 'event scoped manager component' is a bit too vague for me :-(

                      Can you please clarify this a little bit further?

                      Thanks,

                      Mark

                      • 8. Re: How do I reload a Data Model on a SessionBean everytime
                        andygibson

                        I think what pete is getting at is that you are currently trying to outject a Page scoped object from a session scoped bean.

                        By putting the outjection and factory in a separate page scoped bean (the page scoped manager) you are splitting the problem up. Your page scoped users are not being generated from a session scoped bean and you aren't having to deal with two scopes in the same bean.

                        Something like :

                        @Name("userList")
                        @Stateful
                        @Scope(PAGE)
                        @Restrict("#{identity.loggedIn}")
                        public class UserList implements UserListInterface {
                        
                         @Out
                         @DataModel("users")
                         private List<User> users;
                        
                         @Factory("users")
                         public List<User> getUsers() { }
                        
                        }
                        


                        Cheers,

                        Andy



                        • 9. Re: How do I reload a Data Model on a SessionBean everytime
                          mteichmann

                          thanks, in theory I know what to do now.
                          But @Scope(PAGE) is not allowed when using @Stateful. I try to use CONVERSATION scope, I'll post my solution if I get it to work

                          • 10. Re: How do I reload a Data Model on a SessionBean everytime
                            andygibson

                            Actually, that's my mistake (re the @Statefule annotation)

                            It doesn't need to be stateful, you aren't holding the state from one request to the next since it is page scoped. Once the page has hit the browser, the user list has gone since it is page scoped which is what the goal of the post was about.

                            • 11. Re: How do I reload a Data Model on a SessionBean everytime
                              andygibson

                              Also, I think there is another small problem with the code you have there. I could be wrong on this, so hopefully someone will correct me if that is the case.

                              You have outjection on the datamodel for users, and also return a value for the factory method.

                              When you return the value from the factory, that is outjected as the variable "users". When the method completes, the Seam interceptor then looks for outjected variables and outjects the data model as "users". At that point, you have set it twice. While I don't think it would be a problem at run time, the return value from the factory method is unnecessary, and it should be like :

                              @Name("userList")
                              @Scope(PAGE)
                              @Restrict("#{identity.loggedIn}")
                              public class UserList implements UserListInterface {
                              
                               @DataModel("users")
                               private List<User> users;
                              
                               @Factory("users")
                               public void getUsers() {
                               users = entityManager.createQuery(....).getResultList();
                               }
                              
                              }
                              
                              


                              Here, you are using the factory to assign the value, and the @Datamodel annotation handles the outjection.

                              For more details see :

                              http://docs.jboss.com/seam/latest/reference/en/html/concepts.html#d0e3302

                              Cheers,

                              Andy