7 Replies Latest reply on Mar 19, 2010 10:15 PM by wachtda.scsi.gmx.ch

    Strange error passing object in EL

    wachtda.scsi.gmx.ch

      hi together


      i have a big and strange problem with object passing in seam with plain tomcat!
      in my bean i have a function which returns a list of positions (position is transient and holds a product entity and a int quantity) an iterate over it in my view with a h:dataTable:


      public List<Position> showPositions() {
           String query = "SELECT p FROM Product p";             
           List<Product> products = em.createQuery(query).getResultList();
           List<Position> positions = new LinkedList<Position>();
                             
           for (Product product : products) {                             
               positions.add(new Position(product, 1));
           }
      }



      to add a product to the basket i have a h:commandButton which invokes a function and passes the given object:


      <h:form>
      <h:dataTable value="#{myBean.showPositions()}" var="position">
           <h:column>
               #{position.name}
           </h:column>
           <h:column>
               <h:commandButton value="Get it" action="#{myBean.addPosition(position)}" />
           </h:column>
      </h:dataTable>
      </h:form>



      this works like a charme, until i try to access one of the products getters in my bean like:


      for (Product product : products) {                             
           // positions.add(new Position(product, 1));
           // check the product group before adding
           if(product.getGroupLabel().equals(productGroupLabel)) {
               positions.add(new Position(product, 1));
           }
      }



      the products are displayed like before BUT NOW THE OBJECT PASSING WITH EL DOESN'T WORK ANYMORE.
      the objects is not passed neither the function is called, it seams like the button click sends a empty request...


      i became despereate by trying to find the error...
      has this something todo with seam and plain tomcat or am i stupid???
      thank you very much for any help/hint!




        • 1. Re: Strange error passing object in EL
          blabno

          Absolutely not a Tomcat problem. Show full source code of bean containing showPoisitons method. Especially scopes are interesting. My guess is that condition in your if changes over postback resulting in different list size.
          Please do caching of your positions list and put your bean to at leas PAGE scope.


          private List<Position> positions;
          
          public List<Position> showPositions() {
               if(positions == null) {
                   String query = "SELECT p FROM Product p";             
                   List<Product> products = em.createQuery(query).getResultList();
                   positions = new LinkedList<Position>();
                                 
                   for (Product product : products) {                             
                       positions.add(new Position(product, 1));
                   }
               }
               return positions;
          }

          • 2. Re: Strange error passing object in EL
            wachtda.scsi.gmx.ch

            hi bernard


            thanks for the quick reply!
            sadly i don't have the code at my site, but the bean is in conversation scope!
            but i spotted some problems with plain tomcat and conversations (i don't have a ejb container),
            the conversation seams not to work!


            my bean is named and configured like this:


            @Name("myBean")
            @Scope(ScopeType.CONVERSATION)
            public class MyBeanImpl implements MyBean
            {
                //...
            }



            maybe i give the @Page scpe a try...



            • 3. Re: Strange error passing object in EL
              blabno

              Daniel, it is absolutely impossible that conversations do not work, because of Tomcat. You must have not used them properly. Having bean put in CONVERSATION scope does not guarantee postback survival. By default conversation is not long-running and thus almost equal to EVENT scope. This may be your case.

              • 4. Re: Strange error passing object in EL
                wachtda.scsi.gmx.ch

                hm, strange, i annotated my functions inside the conversation scoped bean with


                @Begin(join=true)



                and it doesn't work. if i make this the same way on a bean runned inside my ejb container (jboss as) then it works like a charme...

                • 5. Re: Strange error passing object in EL
                  blabno

                  How do you invoke those methods ? In order for anotations to work, invocations must be done on proxies, and not on the bean itself. Note that EL expressions work on proxies, injected beans are proxies in fact, Component.getInstance also returns proxies. But :


                  @Name("foo")
                  public class Foo {
                  
                      public void bar() {
                          bar2();
                      }
                  
                      @Begin
                      public void bar2(){}
                  }
                  



                  If you call #{foo.bar} then no conversation will be started because bar is invoked on proxy, but bar2 is invoked directly on bean.

                  • 6. Re: Strange error passing object in EL
                    wachtda.scsi.gmx.ch

                    ok, this is correct. but my bean looks like this:




                    @Name("myBean")
                    @Scope(ScopeType.CONVERSATION)
                    public class MyBeanImpl implements MyBean
                    {
                        @Begin(join=true)
                        public List<Position> showPositions() {
                             String query = "SELECT p FROM Product p";             
                             List<Product> products = em.createQuery(query).getResultList();
                             List<Position> positions = new LinkedList<Position>();
                                           
                             for (Product product : products) {                             
                                 positions.add(new Position(product, 1));
                             }
                        }
                    }
                    



                    and the function in my bean is called in the el like this,
                    so i don't see the problems:


                    <h:form>
                    <h:dataTable value="#{myBean.showPositions()}" var="position">
                         <h:column>
                             #{position.name}
                         </h:column>
                         <h:column>
                             <h:commandButton value="Get it" action="#{myBean.addPosition(position)}" />
                         </h:column>
                    </h:dataTable>
                    </h:form>



                    what is could be the problem?

                    thanks for your effort, bernard!
                    :-)

                    • 7. Re: Strange error passing object in EL
                      wachtda.scsi.gmx.ch

                      I found the problem!
                      The problem is not the access of the bean, but the request parameter which i use to check the property.


                      @RequestParameter(value="cat")
                      private String productGroupLabel;
                      



                      If i check the property with a common String, the problem does not occur.
                      I have seam url rewriting configured like this:



                       <page view-id="/products.xhtml">
                            <rewrite pattern="/wunschbuch" />
                            <rewrite pattern="/wunschbuch/{cat}" />
                            <rewrite pattern="/wunschbuch/{cat}/" />
                            <navigation from-action="#{basketAction.addProductToBasket(position)}">
                                 <redirect view-id="/basket.xhtml"/>
                            </navigation>
                       </page>



                      Could the problem be my url rewriting configuration?
                      (Beside this problem, I don't got the conversation to run, only session works)

                      Any hints? Thank you...