webtier@glassfish.java.net

Re: [webtier] using EJBs inside JSF pages - is it possible ?

From: <webtier_at_javadesktop.org>
Date: Mon, 14 Dec 2009 03:33:49 PST

Yes, it is! You can do it through CDI.

// Your EJB -------------------------------------
@Named("myEJB")
@Stateful
@SessionScoped
public class MyEJB {
  public void aMethod() {
    // do something interesting here...
  }
}
// ------------------------------------------------

<!-- Your JSF view ----------------------------- -->
<html xmlns:h="...">
  <h:body>
    <h:form>
      <!-- Another things in your form -->
      <h:commandButton value="Do something..." action="#{myEJB.aMethod}" />
    </h:form>
  </h:body>
</html>
<!-- ---------------------------------------------- -->
 You still need an empty "beans.xml" in your WEB-INF, in case of a pure WebApp, or both WEB-INF and META-INF, if your project a full EnterpriseApp.

Maybe you'll need to fix something in this code but basically it works. For more information you can look for JCDI (or just CDI) or WebBeans on google. Try to give a look on the last entries in Gavin King's and Adam Bien's blog.
You'll find a lot of information about it specially on Gavin King's blog.
[Message sent by forum member 'yellowbike' ]

http://forums.java.net/jive/thread.jspa?messageID=376371