8 Replies Latest reply on Jul 21, 2006 1:01 PM by justinwalsh

    Problem starting converstaion and setting state from HTTP GE

      I have a simple page using the standard login form using j_security_check. JBoss is configured to authenticate against an LDAP server. Once authenticated the <welcome-page> is rendered and it simply does this...

      <meta http-equiv="Refresh" content="0; URL=extract/index.jsf">

      When index.jsf is rendered I need some data retrieved from the database so I can display it immeditately. That's where I have a problem. I can't get the data loaded from the database and displayed in my page.

      I've tried all recommendations in Section 3.3 of the SEAM documentation. Nothing works. I'm sure I'm doing something wrong. Any ideas.

      Here's the code...
      @Stateful
      @Name("package")
      public class PackageAction implements Package {
      
       @DataModel
       private List<PackageEntity> packages = null;
      
       @PersistenceContext(unitName="DC")
       private EntityManager em;
      
      
       public PackageAction() {}
      
       @Begin(join=true) @Create
       public void load() {
       packages = em.createQuery("from PackageEntity").getResultList();
       }
      
       @Destroy @Remove
       public void destroy() {}
      }
      

      I even added a pages.xml file like so...
      <pages>
       <page view-id="/extract/index.jsf" action="#{package.load}"/>
      </pages>
      

      and that doesn't work either.

      Here's the web page...
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <f:view xmlns:h="http://java.sun.com/jsf/html"
       xmlns:f="http://java.sun.com/jsf/core"
       xmlns:c="http://java.sun.com/jstl/core"
       xmlns:ui="http://java.sun.com/jsf/facelets">
      
       <ui:composition>
       <f:loadBundle basename="bundle.packageTable" var="pkgHeaders" />
      
       <h:form>
       <h:dataTable value="#{packages}" var="pkgEntity" styleClass="dataTableBody" headerClass="dataTableHeader"
       rowClasses="evenRow,oddRow" cellspacing="0">
       <h:column>
       <f:facet name="header">
       <h:outputText value="#{pkgHeaders.id}" />
       </f:facet>
       <h:outputText value="#{pkgEntity.id}" />
       </h:column>
       <h:column>
       <f:facet name="header">
       <h:outputText value="#{pkgHeaders.status}" />
       </f:facet>
       <h:outputText value="#{pkgEntity.statusCode}" />
       </h:column>
       <h:column>
       <f:facet name="header">
       <h:outputText value="#{pkgHeaders.user}" />
       </f:facet>
       <h:outputText value="#{pkgEntity.user.username}" />
       </h:column>
      
       ...
      
       </h:dataTable>
       </h:form>
       </ui:composition>
      </f:view>
      


      Thanks.
      Eric Ray

        • 1. Re: Problem starting converstaion and setting state from HTT

          dumming down the question...i really need to do this when the pages loads and without the user doing anything

          public void load() {
          packages = em.createQuery("from PackageEntity").getResultList();
          }

          then display the result of the query in a dataTable.

          Thanks.
          Eric Ray

          • 2. Re: Problem starting converstaion and setting state from HTT
            pmuir

            Perhaps

            "c_eric_ray" wrote:
            @Stateful
            @Name("package")
            public class PackageAction implements Package {
            
             @DataModel
             private List<PackageEntity> packages;
            
             @PersistenceContext(unitName="DC")
             private EntityManager em;
            
             public PackageAction() {}
            
             // @Begin(join=true) @Create
             @Factory("packages")
             // @Begin(join=true) // if you want AS Well
             public void load() {
             packages = em.createQuery("from PackageEntity").getResultList();
             }
            
             @Destroy @Remove
             public void destroy() {}
            }
            



            • 3. Re: Problem starting converstaion and setting state from HTT

              I resolved this using Page Actions. Although, I might try what you suggest petemuir just to see what happens. My guess is that it won't work. I think the problem is the the Seam component "package" not "packages" is not being referenced in the web page so it never gets instantiated and the @Create method is never called. This particualr page only references the @DataModel "packages" property. Since the component is never instantiated I have to use a page action to get the behavior I need.

              This may not be acurate, but it appears to be what is happening.

              Thanks.
              Eric Ray

              • 4. Re: Problem starting converstaion and setting state from HTT
                pmuir

                You have a variable packages (a datamodel) which you outject (using @DataModel) from PackageAction. You have a view which contains a datatable with a value #{packages}. Placing a Factory for "packages" on the on load() method will cause it to be run if the context variable packages is null (which I assume it is?). As the packages Factory method is part of the package seam component running it will cause the package component to be instantiated.

                I'm assuming you don't have seam naming conflicts going on.

                Try it and see ;)

                • 5. Re: Problem starting converstaion and setting state from HTT

                  Thanks petemuir. Worked like a charm. I though I could only use the @Factory if I was using jBPM. No I know better.

                  Thanks.
                  Eric Ray

                  • 6. Re: Problem starting converstaion and setting state from HTT

                    Eric

                    How did you resolve this using page actions? From your original post you seem to already have a page action What did you change?

                    <pages>
                     <page view-id="/extract/index.jsf" action="#{package.load}"/>
                    </pages>
                    


                    • 7. Re: Problem starting converstaion and setting state from HTT

                      I was using a url fragment and that is incorrect. You should use the filename of the view that contains the component in question.

                      However, petemuir suggested an even cleaner solution (IMO) because it's contained in the code and alleviates the need for the xml file. Here's what I did instead of using the pages.xml file.

                       @DataModel
                       private List<PackageEntity> packages;
                      
                       @Factory("packages")
                       public void load() {
                       packages = em.createQuery("from PackageEntity").getResultList();
                       }
                      


                      the load() method, annotated with @Factory("packages"), is used to initalize the "named" property (that property being "packages"). This is done before the page is rendered making this a great way to initialize state before the page is display.

                      Eric Ray

                      • 8. Re: Problem starting converstaion and setting state from HTT

                        Thanks