Firstly my aim was to use a basic JPA/EJB 3.0 entity model project and JSF 2.0 View project. I am going to skip over the model setup as that support has been in JDeveloper for some time, but in short we use the SCOTT schema and create a Emp JPA Entity and an EJB Session Facade in the model project accepting all the defaults.
Note: I created a Java EE Web Application which seems to be the default one I needed for this setup.
The plan here was to use DI (Depency Injection) for the EJB session bean within my managed beans and for my view pages to use Facelet *.JSF pages. I could of used a *.XHTML pages but I feel more comfortable in the JSP world versus HTML world.
1. In the ViewController project create a new JSF page using "File -> New -> Web Tier-> JSF/Facelets -> Page". In this example I created a file called "pages/Emps.jsf". Once created leave it as is we will return to it soon.
Note: I could create the managed bean to serve this page at the same time on the wizard page but I choose not to at this stage, rather do it by hand.
2. Create a new java class called "EmpManagedBean" in a package as follows "view".
3. Replace the code in the empty class with code as follows. Be sure you edit the reference to the EJB session bean to the correct session bean name from the model project.
package view; import java.util.List; import java.util.logging.Logger; import javax.annotation.PostConstruct; import javax.faces.bean.ManagedBean; import javax.ejb.EJB; import model.Emp; import model.EmpSessionEJBLocal; @ManagedBean (name = "empBean") public class EmpManagedBean { @EJB EmpSessionEJBLocal empSessionEJBLocal; private Logger logger = Logger.getLogger(this.getClass().getSimpleName()); @PostConstruct public void init() { emps = empSessionEJBLocal.getEmpFindAll(); } private List<Emp> emps; public EmpManagedBean() { } public void setEmps(List<Emp> emps) { this.emps = emps; } public List<Emp> getEmps() { return emps; } }
Note: Auto import as required.
4.Now at this point return to Emps.jsf and add code as follows.
<?xml version='1.0' encoding='windows-1252'?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <f:view xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> <html xmlns="http://www.w3.org/1999/xhtml"> <h:head> <title>Employees</title> </h:head> <h:body> <h3 style="font-family: arial; font-variant: small-caps; color: #336699"> Employees </h3> <h:form> <h:dataTable var="row" value="#{empBean.emps}" border="1"> <h:column> <f:facet name="header">Empno#</f:facet> #{row.empno} </h:column> <h:column> <f:facet name="header">Ename</f:facet> #{row.ename} </h:column> </h:dataTable> </h:form> </h:body> </html> </f:view>
Note: The CE insight is available for the managed bean within the JSF page ensuring you get the correct property name you need to use.
5. Add the library "Java EE 1.5 API" so the DI code will compile without issues. There may be a smaller library then this but it wasn't obvious from the list.
The rest , such as web.xml setup etc should be setup for us , so no need to do anything there.
6. Run Emps.jsf and verify runtime output as follows.
The JDeveloper log window should show some output as follows once the page is launched.
Run startup time: 12439 ms.
[Application JavaEETestApp deployed to Server Instance IntegratedWebLogicServer]
Target URL -- http://127.0.0.1:7101/javaee-jsf/faces/pages/Emps.jsf