3 Replies Latest reply on Nov 23, 2009 6:48 PM by fmontezuma

    Injection fail, design problems

    fmontezuma

      Hi!


      I have an action bean that is getting too big because its page has some tricky manipulations.


      Trying to divide this bean, I created a property in it called events that instantiate another class called here ActionEvents (to manipulate form fields events).


      I have something like this:


      @Name("action")
      public class Action {
         @In
         private EntityManager entityManager;
      
         private ActionEvents events;
      
         public Action() {
            this.events = new ActionEvents(this);
         }
      
         // other methods   
      
         // getters and setters
      }



      public class ActionEvents {
      
         private Action action;
      
         public ActionEvents(Action action) {
            this.action = action;
         }
      
         public void select_onclick()
         {
            action.methodA();
            action.methodB();
         }
      }



      And the page:


      ...
      <h:selectOneRadio id="select">
         <s:selectItems ...>
         <a:support event="onclick" actionListener="#{action.events.select_onclick}" />
      </h:selectOneRadio>
      ...
      



      But when methodA is called inside select-onclick() the entityManager is null.


      Howto solve this? Does this design looks too bad?


      Thanks in advance!

        • 1. Re: Injection fail, design problems
          niox.nikospara.yahoo.com

          Hello,


          I would suggest creating the ActionEvents in a @Create method:


          @Name("action")
          public class Action {
             @In
             private EntityManager entityManager;
          
             private ActionEvents events;
          
             public Action() {
             }
          
             @Create
             public create() {
                this.events = new ActionEvents(this);
             }
          
             // other methods   
          
             // getters and setters
          }
          

          • 2. Re: Injection fail, design problems
            fmontezuma

            I tried that already... It doesn't work too.


            I mean, I can't get EntityManager or FacesContext injected in my Action bean.


            And the ActionBean is conversation scoped. I missed typing that.


            Any other advice?


            Thanks

            • 3. Re: Injection fail, design problems
              fmontezuma
              I tried something else, injecting Action inside ActionEvents.

              Seems that Seam doesn't try to inject objects when I use the composition pattern in the EL:
              #{action.events.select_onclick}.

              I named my ActionEvents to actionEvents and used the EL #{actionEvents.select_onclick} to use it and now it works.