mardi 22 février 2011

Simple exemple : JSF 2.0, PRIMEFACES 2.2.RC, EJB 3.1, JPA 2

In this tutorial we are going to start developing our rich web application using JSF 2 , PRIMFACES , EJB and JPA
we can create the project  using your preferred IDE ,the most convenient method of creating a new project is to use an IDE that supports it (for example, Eclipse with plugin, NetBeans, JDeveloper)
I will explain all of the steps to build an empty web application project using netbeans and glassFish 3 .
A simple insert into phone_book data base and show the list of ccontact saved here the script of creation

CREATE TABLE contact (
    id integer NOT NULL,
    name character varying(20),
    surname character varying(20),
    mobile_phone character varying(15),
    email character varying(100)

    ADD CONSTRAINT contact_pkey PRIMARY KEY (id);
Download and install
Note - it is required to use java JDK 6  : to check for java installation in windows or linux use " java -version "  in command line

Creating the project

Here we will create the web application EAR , Open netBeans and go to
File > New Project > Java EE > Entreprise Aplication > Next
Put the name (PhoneBook for exemple) and location for your project go to next
NetBeans use GlassFish 3 default server the screen chould be like :

Netbeans create the structure of web application project
Now we will add JSF 2 :
Got to BookPhone-War > right click with mouse > in Categories go to Framwork > Java Server Face
( if you dont have jsf option framwork you need to update netBeans plugin go to tools > plugins ) and confirm add librairie

the web.xml is update and should look :

        Faces Servlet
        Faces Servlet

Now we will create jpa entity from data base

In PhoneBook-ejb go to source package > right click > New > Persistance > Entity classes from data base > next
We will create connection using jpa to database
Data source > choise New DataSource > put name like phone_book into jndi name input > next ower New Data Base connection should look

Than choise the data base schema must be public select contact table from availabe table > enter the entity package like the Contact entity Class is
package org.project.entity;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Table(name = "contact")
public class Contact implements Serializable {
    private static final long serialVersionUID = 1L;
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Column(name = "name")
    private String name;
    @Column(name = "surname")
    private String surname;
    @Column(name = "mobile_phone")
    private String mobilePhone;
    @Column(name = "email")
    private String email;

    public Contact() {

    public Contact(Integer id) { = id;

    public Integer getId() {
        return id;

    public void setId(Integer id) { = id;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getSurname() {
        return surname;

    public void setSurname(String surname) {
        this.surname = surname;

    public String getMobilePhone() {
        return mobilePhone;

    public void setMobilePhone(String mobilePhone) {
        this.mobilePhone = mobilePhone;

    public String getEmail() {
        return email;

    public void setEmail(String email) { = email;

    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;

    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Contact)) {
            return false;
        Contact other = (Contact) object;
        if (( == null && != null) || ( != null && ! {
            return false;
        return true;

    public String toString() {
        return "org.project.entity.Contact[id=" + id + "]";


Now we will create the first welcome page contains a form to add person

2 commentaires:

  1. Can you please provide a full version of the article?

  2. See the title of the Article :) thanks