10 Replies Latest reply on Jul 24, 2008 11:29 PM by ericjava.eric.chiralsoftware.net

    Noob: Not seeing entity data in xhtml page

    dhcinc

      I am not able to work out why I cannot see data from an entity class in my xhtml file (i.e. EL expression)



      1. seam 2.0.2SP1, jboss 4.2.2GA, linux

      2. I used seam-gen to create the project

      3. I created a Java class file with the appropriate @Entity and @Name annotations. The entity class has two getter/setters - one an integer and one a String.

      4. I used the home.xhtml as a template and added EL references to the instance values of the entity class.

      5. When I look at the page there are no values for the EL references.

      6. The ant build is successful and there is nothing in the server.log file (of interest).



      In other words, I have a class 'Foo' with getter/setters on an instance value 'bar'. When I reference #{foo.bar} in my xhtml file the result is empty.


      Ideas? Is there some other log that would help?

        • 1. Re: Noob: Not seeing entity data in xhtml page
          ericjava.eric.chiralsoftware.net

          If you post your class file and the xhtml file, we can figure it out.


          One point on design: the first Seam project I did, I put both @Name and @Entity on my entity classes.  I never did that again.  I found it confusing to have the same things (entities) being managed by both the persistence entity manager AND the Seam component manager.  I stopped doing that, and now all entities are managed by Seam components which are not entities themselves.  95% of these are POJOs, not session beans.  That made it a lot clearer and works well.  If you want to be able to refer to your entity by its simple name, then you can go ahead and outject it.


          -----------


          Android seminar

          • 2. Re: Noob: Not seeing entity data in xhtml page
            dhcinc

            Thanks for the quick response.


            This is the entity class:


            import java.io.Serializable;
            
            import javax.persistence.Entity;
            
            @Entity
            public class Test implements Serializable {
                 private static final long serialVersionUID = 3109852436898487119L;
                 private String text;
                 private int number;
                 
                 public Test() { 
                      this.text = "Foo was here";
                      this.number = 42;
                 }
                 
                 public String getText() {
                      return this.text;
                 }
                 
                 public void setText(String text) {
                      this.text = text;
                 }
                 
                 public int getNumber() {
                      return this.number;
                 }
            }



            and this is the xhtml:


            <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
                 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <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:rich="http://richfaces.org/rich"
                 template="layout/template.xhtml">
            
            <ui:define name="body">
                 The test text is '#{Test.text}' and the number is #{Test.number}.
            </ui:define>
            
            </ui:composition>

            • 3. Re: Noob: Not seeing entity data in xhtml page
              ericjava.eric.chiralsoftware.net

              Well, obviously it's not going to show up.


              Here's what's going on:


              When you use an EL expression in your XHTML, you are looking for things within the universe of Seam components.  I believe there are only four ways a thing can be introduced into the universe of Seam components:




              1. It has a @Name annotation on the class, AND the class is within a JAR file that is being scanned by the Seam component loader (ie, has a seam.properties file in it)

              2. It is created by introducing it in the components.xml file

              3. It is outjected by something which is a Seam component, using the @Out annotation

              4. It is a built-in Seam component, and in fact, all of these are created by using mechanisms 1-3 above.



              So, going back to your code:



              1. Does the Test entity have a @Name annotation?  No.

              2. Is it created in components.xml?  No.

              3. Is some other component outjecting it?  No.

              4. Obviously, it's not a built-in Seam component.



              So, the EL looks at #{test in the XHTML and it evaluates to null.  EL stops silently in a condition like that and gives no output.  That's actually a nice feature.  It means I can say, #{customer.address.city}, and if address is null, it just outputs nothing (as opposed to a NPE), so I don't need to put null tests all over the place.


              What can you do to fix this?


              I usually create a POJO which exists to manage my entities.  I might create something like this (off the top of my head, not tested):


              @Name("testManager")
              public class TestManager {
              
                 private Test test;
              
                 public Test getTest() {
                    if(test == null) test = new Test();
                    return test;
                 }
              }



              And then you could say:


              This is the value: <h:outputText value=#{testManager.test.number}/>



              I would normally also make the testManager component conversation scope, by saying:


              @Name("testManager")
              @Scope(ScopeType.CONVERSATION)
              public class TestManager {



              Does this make sense?


              ------------


              JBoss Seam training

              • 4. Re: Noob: Not seeing entity data in xhtml page
                dhcinc

                Again, thanks for the assist and the great explanation.
                Unfortunately, I am still not seeing anything.


                Here's what I did:



                • I added @Name(test) to the Test entity class

                • I created a ModelManager class as per your suggestion supplying both the @Name and @Scope annotations.

                • I changed the reference to
                  <h:outputText value="#{modelManager.test.number}"/>




                but still nothing shows up.


                I then created a new project using seam and put these files in there. Still no luck.

                • 5. Re: Noob: Not seeing entity data in xhtml page
                  ericjava.eric.chiralsoftware.net

                  First, I advise you not to put the @Name annotation on your entity classes.  I find that gets too confusing (as we can see here).


                  Second, what does your ModelManager class look like?

                  • 6. Re: Noob: Not seeing entity data in xhtml page
                    dhcinc

                    Ok, I took the @Name off the entity class.


                    Here is the ModelManager class:


                    import org.jboss.seam.ScopeType;
                    import org.jboss.seam.annotations.Name;
                    import org.jboss.seam.annotations.Scope;
                    
                    @Name("modelManager")
                    @Scope(ScopeType.CONVERSATION)
                    public class ModelManager {
                         private Test test = null;
                         
                         public Test getTest() {
                              if( test == null ) test = new Test();
                              return test;
                         }
                    }



                    • 7. Re: Noob: Not seeing entity data in xhtml page
                      ericjava.eric.chiralsoftware.net

                      Hmm, it looks right to me at this point.  Something else must be going on.  Could you put a logging statement in getTest() and also in the ModelManager constructor, just to see if these are getting called at all?  Can you see in the logs if the modelManager component is being registered with Seam?


                      Also, this could be a problem with the project framework.  Is there a seam.properties file?  Are filters etc set up correctly?

                      • 8. Re: Noob: Not seeing entity data in xhtml page
                        tony.herstell1

                        When you server runs up look for VERY useful error messages.
                        You should see seam elaborating the bean(s) you have created.

                        • 9. Re: Noob: Not seeing entity data in xhtml page
                          dhcinc

                          I found the problem. Thanks to everyone for pitching in.
                          The fixes suggested by Eric H created an out of memory condition on the jboss server which I found based on Tony Herstell's comments.


                          Hurray! I have a small, but working, seam application.

                          • 10. Re: Noob: Not seeing entity data in xhtml page
                            ericjava.eric.chiralsoftware.net

                            It created an out of memory condition?  I'm very curious about that.  What caused it?