1 Reply Latest reply on Feb 5, 2009 5:49 PM by wrzep

    Assign null to a component or entity

      Hi there,


      I've a problem with simple page. This page should only display the properties of an entity. I should mention here that this entity has several associations to ohter entities and some of the properties of those associated entities are also displayed.


      So it's something like this:


      <h:outputText value="#{entity.id} />
      <h:outputText value="#{entity.associatedEntity.name} />



      To determine which entity is to be read from database and displayed on the page, I use a request parameter (the page should be available without having a session started). The target of this parameter is given in the component descriptor. The setter of this parameter now tries to parse the parameter and to get the related entity from the database. If it's found, the entity is outjected:



      @Name("backingBean")
      public class BackingBean {
        @Out(required = false)
        private Entity entity;
      
        public void setParameter(String parameter) {
          try {
            entity = entityManager.find(Entity.class, Long.parseLong(parameter);
          } catch(Exception e) {
            entity = null;
          }
        }
      }
      



      It's just a demonstration of my code. It works like this and since I don't have access to my code atm I hope you don't mind.


      This works as expected. Now there is a button on the page which calls a method in another backing bean. This backing bean injects the entity:


      @Name("backingBean2")
      public class BackingBean2 {
        @In
        private Entity entity;
      
        public String methodCalledByButton() {
          //do something with the injected entity
        }
      }



      As you can see the entity is required.


      Since the ID of the entity is parsed from a request parameter, it can happen that the entity can't be resolved thus displaying nothing on the page. If the user now clicks on the button of backingBean2 an exception is thrown since backingBean2 requires the entity.


      I thought I could render the button only if an entity is given:


      <h:commandLink action="#{backingBean2.methodCalledByButton} render="#{entity != null}" />



      But this doesn't work. This one:


      <h:outputText value="#{entity}" />



      shows that the entity is somehow instantiated. And I don't know why. Does anybody know, why this entity is instantiated or how to implement what I want to realize?


      Thanks in advance
      Jens

        • 1. Re: Assign null to a component or entity
          wrzep

          Jens,


          When you first outject the entity it is outjected to the event context. Calling a method on backingBean2 is another event, so this entity is not available there. Use wider scope or conversations to handle this. Alternatively, using a method parameter - bean2.method(entity) in the command button might be a simple solution here.


          Cheers,
          -Pawel