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 DATABASE phone_book

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

ALTER TABLE ONLY contact
    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 :

    
        javax.faces.PROJECT_STAGE
        Development
    
    
        Faces Servlet
        javax.faces.webapp.FacesServlet
        1
    
    
        Faces Servlet
        /faces/*
    
    
        30
    
    
        faces/index.xhtml
    


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 com.project.phone.entity the Contact entity Class is
package org.project.entity;

import java.io.Serializable;
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;

@Entity
@Table(name = "contact")
public class Contact implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @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) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.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) {
        this.email = email;
    }

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

    @Override
    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 ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    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: