Introduction to Ejb3.1 in Cdi (jsr-330)
This tutorial assumes that you already have knowledge on EJB, JSF, maven and injection. These are the steps as well as the code I use to mak...

1.) Create 2 maven projects (web, ejb - has HelloBean class) 2.)
2.) In the web part we need to create 3 critical files inside /WEB-INF folder:
beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans></beans>faces-config.xml
<?xml version='1.0' encoding='UTF-8'?> <faces-config xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" version="2.0"> </faces-config>web.xml
Then our xhtml page, let's name it home.xhtmlXXX - Merchant Gateway javax.faces.PROJECT_STAGE Development home.xhtml javax.faces.DEFAULT_SUFFIX .xhtml javax.faces.application.CONFIG_FILES /WEB-INF/faces-config.xml Faces Servlet javax.faces.webapp.FacesServlet 1 Faces Servlet /faces/* Faces Servlet *.jsf Faces Servlet *.faces Faces Servlet *.xhtml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"> <h:body> <h:outputLabel value="#{helloBean.name}" /> </h:body> </html>
3.) For the second part (ejb), we will create 4 files: 1 java class and 3 xml. Inside resources/META-INF, we need to create(beans.xml, ejb-jar.xml, sun-ejb-jar.xml)
beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans></beans>
ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?> <ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:ejb="http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd" version="3.1"> <module-name>XXX-cg-ejbs</module-name> <display-name>YYY Gateway</display-name> </ejb-jar>
sun-ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sun-ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 EJB 3.0//EN" "http://www.sun.com/software/appserver/dtds/sun-ejb-jar_3_0-0.dtd"> <sun-ejb-jar> <enterprise-beans /> </sun-ejb-jar>
And last, our class file HelloBean.java
import javax.annotation.PostConstruct; import javax.ejb.Stateless; import javax.inject.Named; /** * @author Edward P. Legaspi * @since Jun 5, 2012 */ @Stateless @Named("helloBean") public class HelloBean { private String name; public HelloBean() { name = "edward"; } @PostConstruct public void init() { name = "edward"; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
*You should take note that beans.xml, should be created on both the web and ejb project. After that the JSF front should be able to call the ejb back.
Post a Comment