3 Replies Latest reply on Jun 12, 2008 12:22 PM by nimo stephan

    init Page with @Factory, @Create, @Startup

    nimo stephan Master

      How can I initialize a (xhtml)-page with a value coming from a list without invoking an explicit client-action?


      I want to show a list of data automatically, when the user visits a page.


      With @Factory, @Create-Annotation?


      @Factory
      public List <Persons> initProducts () 
      {return showAllPersonsAtStartup = entityManager.createQuery("select name from Person").getResultList();
      }
      



      when the user comes to that page, the page should automatically renders a datatable and shows the result of the list 'showAllPersonsAtStartup':


      <h:dataTable value="#{showAllPersonsAtStartup}" var="person">
      <h:column>
      <h:outputText value="#{person}"/>
      </h:column>
      </h:dataTable>
      



      And when the user clicks to a 'SelectOneMenu' with the Seam-Tag s:selectItems:


      <s:label>Select Person: </s:label>
      <h:selectOneMenu id="selectOneShift">
        <s:selectItems value="#{showAllPersonsAtStartup}"/>
      </h:selectOneMenu>     
      



      How can I do that?


        • 1. Re: init Page with @Factory, @Create, @Startup
          Nicklas Karlsson Master

          Well, @Factory(foo) should be called if foo is referenced and is found to be null so that's a starting point.


          You could check out ShowOrdersAction in the dvdstore example.

          • 2. Re: init Page with @Factory, @Create, @Startup
            Gena Batalski Newbie

            You did it already. Try



            @Factory("showAllPersonsAtStartup") // not the best name for an entity collection
            



            and use an entity converter for s:selectItems if you wish to know a selection.

            • 3. Re: init Page with @Factory, @Create, @Startup
              nimo stephan Master

              Okay the @Factory works well with that:


              my view:


              <s:label styleClass="label">Products: </s:label>
              <h:selectOneMenu >
              <s:selectItems value="#{initProducts}" var="product" label="#{product[1]}" />
              </h:selectOneMenu>
              



              my session bean:


              private List <Product> listProduct;
              
              @Factory("initProducts")
              public List <Product> initProducts() {
                       
              return listProduct = entityManager.createQuery("select id, description, price from Product").getResultList();
              }
              



              The

              label="#{product[1]

              put the data of the field 'description' in the 'selectOneMenu'. This works well.


              BUT this does not work:


              my view:


              <s:label styleClass="label">Products: </s:label>
              <h:selectOneMenu >
              <s:selectItems value="#{initProducts}" var="product" label="#{product.description}" />
              <s:convertEntity />
              </h:selectOneMenu>
              



              my session bean:


              private Product product;
              public void setProduct(Product product) {
                      this.product = product;
              }
              
              public Product getProduct() {
                   return product;
              }
              private List <Product> listProduct;
              
              @Factory("initProducts")
              public List <Product> initProducts() {
                       
              return listProduct = entityManager.createQuery("select id, description, price from Product").getResultList();
              }
              



              This stacktrace occurs:


              ERROR [viewhandler] Error Rendering View[/Product.xhtml]
              java.lang.NumberFormatException: For input string: "description"
              



              Product is still an entity. What is wrong?