3 Replies Latest reply on Jun 9, 2009 5:28 AM by Wolfgang Knauf

    EJB-Injection from JSF in JBoss 4.2.2

    Nicole Schweighardt Novice

      Hello,

      I useJBoss AS 4.2.2 GA.
      I have got a simple EJB3-project that works. A Testclient simply does an output at the console.
      I get the SessionBeans via the InitialContext:
      Testclient.java

      public static void main(String[] args) {
       Context context;
       try
       {
       context = new InitialContext();
       //Zugriff auf die Session Beans
      ShopWebSite shopWebSiteBean= (ShopWebSite) context.lookup(ShopWebSiteBean.RemoteJNDIName);
      
      ...
      

      Now I have a JSF-application, which should get access to the same sessionbeans from the project above.
      I read that @EJB() is not available in this JBossversion so I thought that the ejb injection should work in the same way as in the project above. So I have following code in the JSF-managed-bean "ShopBean" in the constructor:
      private ShopBean (){
      
       context = new InitialContext();
       try
       {
       ShopWebSite site = (ShopWebSite)context.lookup(ShopWebSiteBean.RemoteJNDIName);
       }
       catch (NamingException e)
       {
       throw new RuntimeException(e);
       }
       }
      

      But in eclipse there is shown ...type cannot be resolved.
      I have got separate projects in eclipse. One dynamic web project for JSF and an EJB-project. Should I connect them and when yes how to connect them?
      Do there exist any best practises for getting access to the Session Beans? I do not think that it is the best solution to put it into the construktor from the JSF-managedbeans?
      Thank you for your answers.
      NSchweig

        • 1. Re:  EJB-Injection from JSF in JBoss 4.2.2
          maxime thozac Newbie

          Hello,

          For the first question on how to integrate an external project to a current project in Eclipse, it' simple !

          Just click on the project, where the link with the EJB project is not done.

          Click right, and choose something like this "update or change or modify build path" and go to the panel "project", in this panel click "add", and add a link to an another project in Eclipse, here, your EJB project ...

          After this done, the autocorrection will accept, the call of the bean in the current project.

          For the second point, i a m trying too to do Injection or link to a session bean EJB3 (facade or session) in a JSF view, but i have still even problem ...

          Tell me if you know how to do !

          • 2. Re:  EJB-Injection from JSF in JBoss 4.2.2
            Nicole Schweighardt Novice

            Hi,

            I now work with JBoss 5 but I thinks the following is the solution for 4.2.2 If you have any problems with that please ask again.

            in EJB "ShopWebSiteBean":

            public @Stateless class ShopWebSiteBean implements ShopWebSite {
            
             public static final String RemoteJNDIName = ShopWebSiteBean.class.getSimpleName() + "/remote";
            
            ..}



            from JSF-ManagedBean: "ShopBean":

            private ShopWebSite getShopWebSiteBean() {
             try {
             InitialContext ctx = new InitialContext();
             return (ShopWebSite) ctx.lookup("ShopWebSiteBean/remote");
             } catch (Exception e) {
             e.printStackTrace();
             throw new RuntimeException("couldn't lookup ShopWebSiteBean", e);
             }
             }



            Hope it helps,
            NSchweig

            • 3. Re:  EJB-Injection from JSF in JBoss 4.2.2
              Wolfgang Knauf Master

              Hi,

              injection into JSF managed beans works in JBoss 4.2.3 and 5.0. So an "@EJB" annotation should be enough. This will only fail if the JSF implementation is not the one bundled with JBoss.

              Best regards

              Wolfgang