Tech-Today
Using EJB3.1 as the backing bean for JSF using 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 make it work.
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
XXX - 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
Then our xhtml page, let's name it home.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.
-
Custom Application Context Or Name In Jboss Or Wildfly
As far as I know there are 3 ways to deploy an application in JBoss or Wildfly with a custom application context name. 1.) By adding jboss-web.xml in your web project WEB-INF folder; <?xml version="1.0" encoding="UTF-8"?> <jboss-web xmlns="http://www.jboss.com/xml/ns/javaee"...
-
How To Setup Seam3-security In Jboss 7
Recently, I've done some research on several Java Security Framework that can perform authentication, authorization and cryptography. I've worked with Apache Shiro, it's really good and complete but I've found several problems like there's...
-
Commonly Use Java Namespace
Below are the commonly use namespace in java: xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui" xmlns:s="http://jboss.org/seam/faces"...
-
How To Integrate Apache Shiro With Javaee6
This tutorial will show the readers, how I was able to integrate Apache Shiro with JavaEE6. My technology Stack: JavaEE6, Glassfish3.1.2.2, Apache Shiro1.2 Steps: 1.) Create a maven project and include the following dependency. <dependencies> <dependency>...
-
How To Call A Stateless Ejb From A Spring Rest Servlet Component
In my recent project I was ask to develop a rest servlet (using jersey) in a spring web project that would call an stateless EJB, where the business logic is. This project is deployed on glassfish. After several hours I was able to figure out how...
Tech-Today