6 Replies Latest reply on Nov 30, 2006 3:46 PM by klejs

    @EJB Injection in JSF Managed Bean

      Hi all ejb3 gurus,

      i' am trying to use @EJB injection in a jsf managed bean class and i use the jbossas version 4.0.5.

      I want to know if this scenario can work?

      --- EJB 3 Codes
      @javax.ejb,Remote
      public Interface Service{
       public String meth1(Object o);
      }
      
      @javax.ejb.Stateful
      public class ServiceImpl implements Service{
       @PersistenceContext
       private EntityManager em;
       public String meth1(Test t){
       em.persist(t);
       return "success";
       }
      }
      
      the String "success" is for the redirection
      
      --- JSF Managed Bean class
      public class ManagedBean{
       private String field1="";
      
       @EJB
       private Service s1;
      
       public String getField1(){return field1;}
       public void setFied1(String f){field1 = f;}
      
       public String controller(){
       if(field1==null){
       return null;
       }else{
       return (s1.meth1(new Test(getField1()));
       }
       }
      }


        • 1. Re: @EJB Injection in JSF Managed Bean

          Now i know that this scenario can not work, because jbossas 4 does not support @EJB Injection in a JSF Managed Bean class. In JSF Managed Bean you can now only use JNDI ENC javax.naming.InitialContext Inteface to look up session's interface compenents.

          -- EJB 3 Codes
          
          @javax.ejb,Remote
          public Interface Service{
           public String meth1(Object o);
          }
          
          @javax.ejb.Stateful
          public class ServiceImpl implements Service{
           @PersistenceContext
           private EntityManager em;
           public String meth1(Test t){
           em.persist(t);
           return "success";
           }
          }
          
          the String "success" is for the redirection
          
          
          --- JSF Managed Bean class
          
          public class ManagedBean{
           private String field1="";
           private Service s1 = null;
          
           public ManagedBean(){
           InitialContext ctx = new InitialContext(); //JNDI ENC
           s1 = (Service)ctx.lookup("ServiceImpl/remote");
           }
          
           public String getField1(){return field1;}
           public void setFied1(String f){field1 = f;}
          
           public String controller(){
           if(field1==null){
           return null;
           }else{
          
          return (s1.meth1(new Test(getField1()));
          
           }
           }
          }


          • 2. Re: @EJB Injection in JSF Managed Bean
            gquintana

             

            "sisepago" wrote:
            bossas 4 does not support @EJB Injection in a JSF Managed Bean

            I have the same problem, where did you find this information?

            • 3. Re: @EJB Injection in JSF Managed Bean
              ssilvert

              This is a JEE5 feature. It is not supported until JBoss 5. You can download the JBoss 5 beta at http://sourceforge.net/project/showfiles.php?group_id=22866&package_id=16942&release_id=464702

              Also see "Resource Injection for Managed Beans" in this article:
              http://wiki.jboss.org/wiki/Wiki.jsp?page=JBossWithJSFCDDL

              Stan

              • 4. Re: @EJB Injection in JSF Managed Bean
                zzeuxis

                This might be a stupid question ... but I'm trying to accomplish the exact same thing : calling a Stateful Bean from a JSF backing bean

                The problem is that I get a "new" (i.e. with no attributes) bean (the stateful one) each time I do the jndi lookup.
                I thought states (i.e. attributes) should be preserved inside a session and thus "the same" Stateful bean should be returned ? Is that correct ?

                The strangest thing is that if I declare my bean Stateless instead of Stateful this seems to be working because attributes created are still there where I get the object again with jndi lookup ...

                Any help from a more ejb3-sawy user ?

                • 5. Re: @EJB Injection in JSF Managed Bean
                  klejs

                  If you don't want your code full of EJB dependent code you can use a VariableResolver instead to "inject" your EJBs into your Managed Beans. It would look something like this:

                  public class SessionBeanVariableresolver extends VariableResolver {
                  
                   private Context context;
                  
                   protected final VariableResolver resolver;
                  
                   public SessionBeanVariableResolver(VariableResolver resolver) {
                   super();
                   this.resolver = resolver;
                   try {
                   context = new InitialContext();
                   }
                   catch(NamingException e) {
                   // handle exception
                   }
                   }
                  
                  
                   @Override
                   public Object resolveVariable(FacesContext context, String name)
                   throws EvaluationException {
                  
                   // Return if object found with regular resolver
                   Object result = resolver.resolveVariable(context, name);
                   if(result != null) {
                   return result;
                   }
                  
                   try {
                   result = context.lookup(name);
                   }
                   catch (NamingException e) {
                   // handle exception
                   }
                  
                   return result;
                   }
                  }
                  


                  In your backing beans you should have a setter for the EJB like this:

                  public Class MyManagedBean() {
                  
                   private MyEjbInterface bean;
                  
                   public void setMyEjbInterface(MyEjbInterface bean) {
                   this.bean = bean;
                   }
                  }


                  And finally in your faces config you should have an entry for your variable resolver:

                  <variable-resolver>my.package.SessionBeanVariableResolver</variable-resolver>


                  and on you managed bean you should set a managed property like this:

                  <managed-bean>
                   <managed-bean-name>MyManagedBean</managed-bean-name>
                   <managed-bean-class>my.package.MyBean</managed-bean-class>
                   <managed-bean-scope>request</managed-bean-scope>
                   <managed-property>
                   <property-name>myEjbInterface</property-name>
                   <value>#{path_to_ejb}</value>
                   </managed-property>
                  </managed-bean>


                  • 6. Re: @EJB Injection in JSF Managed Bean
                    klejs

                     

                    The problem is that I get a "new" (i.e. with no attributes) bean (the stateful one) each time I do the jndi lookup.


                    That's not a problem, it works as intended. Every time you look up a stateful session bean in JNDI, a new session is created.

                    /klejs