Tutorial – Desenvolvendo uma Aplicação CRUD para Web a partir ...

infestationwatchΛογισμικό & κατασκευή λογ/κού

28 Οκτ 2013 (πριν από 3 χρόνια και 7 μήνες)

91 εμφανίσεις

Tutorial


Desenvolvendo uma Aplicação CRUD para Web a partir
de um diagrama de classes da UML

1)

Vamos criar uma aplicação que faz realiza as funcionalidades CRUD de todas as entidades
especificadas pelo diagrama de classes abaixo.


2)

Crie um novo Dynamic Web

Project chamado Projeto_Crud configurado com o Hibernate,
JSF e MySQL.

3)

Abra o arquivo persistence.xml dentro da pasta src/META
-
INF e preencha
-
o com o
seguinte código:

<?
xml

version
=
"1.0"

encoding
=
"UTF
-
8"
?>

<
persistence

version
=
"2.0"

xmlns
=
"http://java.sun
.com/xml/ns/persistence"

xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema
-
instance"

xsi:schemaLocation
=
"http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
>


<
persistence
-
unit

name
=
"Projeto_Crud"

transaction
-
type
=
"
RESOURCE_LOCAL"
>



<
provider
>
org.hibernate.ejb.HibernatePersistence
</
provider
>



<
properties
>




<
property

name
=
"javax.persistence.jdbc.url"

value
=
"jdbc:mysql://localhost:3306/crud"
/>




<
property

name
=
"javax.persistence.jdbc.user"

value
=
"root"
/>




<
prope
rty

name
=
"javax.persistence.jdbc.password"

value
=
"root"
/>




<
property

name
=
"javax.persistence.jdbc.driver"

value
=
"com.mysql.jdbc.Driver"
/>




<
property

name
=
"hibernate.hbm2ddl.auto"

value
=
"update"
/>




<
property

name
=
"hibernate.show_sql"

value
=
"true"
/>



</
properties
>


</
persistence
-
unit
>

</
persistence
>


4)

Dentro da pasta src do Projeto_Crud vamos criar 4 pacotes: modelo, fachada, converters
e beansgerenciados. Para criar um novo pacote dentro da pasta src, basta clicar com o
botão direito do mouse sobre a p
asta src e ir em New > Other.. > Package.


5)

A estrutura do Projeto deve ficar como mostra a figura abaixo:


6)

Agora, vamos criar as classes em Java que representam esse diagrama. Para isso,
devemos criar 4 classes dentro do pacote modelo: Pessoa, Aluno, Cur
so e Disciplina.
Todas devem conter os atributos especificados no diagrama e as anotações para realizar a
persistência em banco de dados. Segue abaixo o código fonte das 4 classes:

Pessoa.java

package

modelo;


import

java.io.Serializable;

import

javax.pers
istence.*;

import

java.util.Date;


@Entity

@Inheritance
(strategy = InheritanceType.
JOINED
)

@Table
(name =
"pessoa"
)

public

class

Pessoa
implements

Serializable {


private

static

final

long

serialVersionUID

= 1L;

@Id

@Column
(name =
"CPF"
, nullable=
false
)

pri
vate

String
cpf
;

@Basic
(optional =
false
)

@Column
(name =
"NOME"
, nullable=
false
)

private

String
nome
;

@Basic
(optional =
false
)

@Column
(name =
"DATANASCIMENTO"
, nullable=
false
)

private

Date
dataNascimento
;

@Basic
(optional =
false
)

@Column
(name =
"TELEFONE"
,

nullable=
true
)

private

String
telefone
;


public

Pessoa() {

}


public

String getCpf() {


return

cpf
;

}

public

void

setCpf(String cpf) {


this
.
cpf

= cpf;

}

public

String getNome() {


return

nome
;

}

public

void

setNome(String nome) {


this
.
nome

=

nome;

}

public

Date getDataNascimento() {


return

dataNascimento
;

}

public

void

setDataNascimento(Date dataNascimento) {


this
.
dataNascimento

= dataNascimento;

}

public

String getTelefone() {


return

telefone
;

}

public

void

setTelefone(String tel
efone) {


this
.
telefone

= telefone;

}


@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

Pessoa)) {


return

false
;


}


Pessoa oth
er = (Pessoa) object;


if

(!(
this
.getCpf().equals(other.getCpf()))) {


return

false
;


}


return

true
;

}

}


Aluno.java

package

modelo;


import

java.io.Serializable;

import

javax.persistence.*;


@Entity

@Table
(name =
"aluno"
)

public

class

Alu
no
extends

Pessoa
implements

Serializable {


private

static

final

long

serialVersionUID

= 1L;

@Basic
(optional =
false
)

@Column
(name =
"RA"
, nullable=
false
)

private

int

ra
;

@Basic
(optional =
false
)

@Column
(name =
"EMAIL"
, nullable=
false
)

private

String
emai
l
;

@Basic
(optional =
false
)

@Column
(name =
"ESCOLARIDADE"
, nullable=
true
)

private

String
escolaridade
;

@JoinColumn
(name =
"CURSO_ID"
, referencedColumnName =
"ID"
, nullable=
false
)

@ManyToOne

private

Curso
curso
;


public

Aluno() {

}


public

int

getRa() {



return

ra
;

}

public

void

setRa(
int

ra) {


this
.
ra

= ra;

}

public

String getEmail() {


return

email
;

}

public

void

setEmail(String email) {


this
.
email

= email;

}

public

String getEscolaridade() {


return

escolaridade
;

}

public

void

setEscolari
dade(String escolaridade) {


this
.
escolaridade

= escolaridade;

}

public

Curso getCurso() {


return

curso
;

}

public

void

setCurso(Curso curso) {


this
.
curso

= curso;

}


@Override

public

boolean

equals(Object object) {


//
TODO
: Warning
-

this me
thod won't work in the case the id fields are not set


if

(!(object
instanceof

Aluno)) {


return

false
;


}


Aluno other = (Aluno) object;


if

(!(
this
.getCpf().equals(other.getCpf()))) {


return

false
;


}


return

true
;

}

}


C
urso.java


package

modelo;


import

java.io.Serializable;

import

javax.persistence.*;


@Entity

@Table
(name =
"curso"
)

public

class

Curso
implements

Serializable {


private

static

final

long

serialVersionUID

= 1L;

@Id

@GeneratedValue
(strategy = GenerationTyp
e.
IDENTITY
)

@Column
(name =
"ID"
, nullable=
false
)

private

int

id
;

@Basic
(optional =
false
)

@Column
(name =
"NOME"
, nullable=
false
)

private

String
nome
;

@Basic
(optional =
false
)

@Column
(name =
"NOMEDEPARTAMENTO"
, nullable=
false
)

private

String
nomeDepartament
o
;

@Basic
(optional =
false
)

@Column
(name =
"CARGAHORARIA"
, nullable=
true
)

private

int

cargaHoraria
;


public

Curso() {

}


public

int

getId() {


return

id
;

}

public

void

setId(
int

id) {


this
.
id

= id;

}

public

String getNome() {


return

nome
;

}

publ
ic

void

setNome(String nome) {


this
.
nome

= nome;

}

public

String getNomeDepartamento() {


return

nomeDepartamento
;

}

public

void

setNomeDepartamento(String nomeDepartamento) {


this
.
nomeDepartamento

= nomeDepartamento;

}

public

int

getCargaHorari
a() {


return

cargaHoraria
;

}

public

void

setCargaHoraria(
int

cargaHoraria) {


this
.
cargaHoraria

= cargaHoraria;

}


@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

Curso)) {


return

false
;


}


Curso other = (Curso) object;


if

(!(
this
.getId() == other.getId())) {


return

false
;


}


return

true
;

}

}


Disciplina.java


package

modelo;


import

java.io.Serializable;

import

javax.persistence.*;

import

modelo.Curso;


@Entity

@Table
(name =
"disciplina"
)

public

class

Disciplina
implements

Serializable {


private

static

final

long

serialVersionUID

= 1L;

@Id

@GeneratedValue
(strategy = GenerationType.
IDENTITY
)

@Column
(name =

"ID"
, nullable=
false
)

private

int

id
;

@Basic
(optional =
false
)

@Column
(name =
"NOME"
, nullable=
false
)

private

String
nome
;

@Basic
(optional =
false
)

@Column
(name =
"QUANTIDADECREDITOS"
, nullable=
true
)

private

int

quantidadeCreditos
;

@Basic
(optional =
false
)

@Column
(name =
"NOMEPROFESSOR"
, nullable=
true
)

private

String
nomeProfessor
;

@JoinColumn
(name =
"CURSO_ID"
, referencedColumnName =
"ID"
, nullable=
false
)

@ManyToOne

private

Curso
curso
;


public

Disciplina() {

}


public

int

getId() {


return

id
;

}

publi
c

void

setId(
int

id) {


this
.
id

= id;

}

public

String getNome() {


return

nome
;

}

public

void

setNome(String nome) {


this
.
nome

= nome;

}

public

int

getQuantidadeCreditos() {


return

quantidadeCreditos
;

}

public

void

setQuantidadeCreditos(
int

q
uantidadeCreditos) {


this
.
quantidadeCreditos

= quantidadeCreditos;

}

public

String getNomeProfessor() {


return

nomeProfessor
;

}

public

void

setNomeProfessor(String nomeProfessor) {


this
.
nomeProfessor

= nomeProfessor;

}

public

Curso getCurso() {


return

curso
;

}

public

void

setCurso(Curso curso) {


this
.
curso

= curso;

}


@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

Disciplina)
) {


return

false
;


}


Disciplina other = (Disciplina) object;


if

(!(
this
.getId() == other.getId())) {


return

false
;


}


return

true
;

}

}


7)

Agora, vamos criar as classes no padrão Fachada para acesso das classes Pessoa, Aluno,

Disciplina e Curso ao banco de Dados. Iremos criar 5 novas classes dentro do pacote fachada,
sendo: AbstractFacade, PessoaFacade, AlunoFacade, CursoFacade e DisciplinaFacade. Segue
abaixo o código de cada uma dessas classes:

AbstractFacade.java


package f
achada;


import java.util.List;

import javax.persistence.EntityManager;

import javax.persistence.EntityManagerFactory;

import javax.persistence.Persistence;


public abstract class AbstractFacade<T> {


private Class<T> entityClass;


protected static E
ntityManagerFactory emf =
Persistence.createEntityManagerFactory("Projeto_Crud");



public AbstractFacade(Class<T> entityClass) {


this.entityClass = entityClass;


}



protected abstract EntityManager getEntityManager();



public void cr
eate(T entity) {





try {






getEntityManager().getTransaction().begin();


getEntityManager().persist(entity);





getEntityManager().getTransaction().commit();




} catch (Exception e) {





e.printStackTrace();





}


}



public void
edit(T entity) {





try {






getEntityManager().getTransaction().begin();




getEntityManager().merge(entity);





getEntityManager().getTransaction().commit();




} catch (Exception e) {





e.printStackTrace();





}


}



public void remo
ve(T entity) {





try {






getEntityManager().getTransaction().begin();




getEntityManager().remove(getEntityManager().merge(entity));





getEntityManager().getTransaction().commit();




} catch (Exception e) {





e.printStackTrace();





}



}



public T find(Object id) {


return getEntityManager().find(entityClass, id);


}



@SuppressWarnings({ "unchecked", "rawtypes" })


public List<T> findAll() {


javax.persistence.criteria.CriteriaQuery cq =
getEntityManager().g
etCriteriaBuilder().createQuery();


cq.select(cq.from(entityClass));


return getEntityManager().createQuery(cq).getResultList();


}



@SuppressWarnings({ "unchecked", "rawtypes" })


public List<T> findRange(int[] range) {


jav
ax.persistence.criteria.CriteriaQuery cq =
getEntityManager().getCriteriaBuilder().createQuery();


cq.select(cq.from(entityClass));


javax.persistence.Query q = getEntityManager().createQuery(cq);


q.setMaxResults(range[1]
-

range[0]);


q.setFirstResult(range[0]);


return q.getResultList();


}



@SuppressWarnings({ "unchecked", "rawtypes" })


public int count() {


javax.persistence.criteria.CriteriaQuery cq =
getEntityManager().getCriteriaBuilder().createQu
ery();


javax.persistence.criteria.Root<T> rt = cq.from(entityClass);


cq.select(getEntityManager().getCriteriaBuilder().count(rt));


javax.persistence.Query q = getEntityManager().createQuery(cq);


return ((Long) q.getSingleRes
ult()).intValue();


}


}


PessoaFacade.java

package fachada;


import javax.persistence.EntityManager;

import modelo.Pessoa;


public class PessoaFacade extends AbstractFacade<Pessoa> {


private EntityManager em = AbstractFacade.emf.createEntityManager
();



protected EntityManager getEntityManager() {


return em;


}



public PessoaFacade() {


super(Pessoa.class);


}

}



AlunoFacade.java



package fachada;


import javax.persistence.EntityManager;

import modelo.Aluno;


public cla
ss AlunoFacade extends AbstractFacade<Aluno> {


private EntityManager em = AbstractFacade.emf.createEntityManager();



protected EntityManager getEntityManager() {


return em;


}



public AlunoFacade() {


super(Aluno.class);


}

}


CursoFacade.java

package fachada;


import javax.persistence.EntityManager;

import modelo.Curso;


public class CursoFacade extends AbstractFacade<Curso> {


private EntityManager em = AbstractFacade.emf.createEntityManager();



protected EntityMana
ger getEntityManager() {


return em;


}



public CursoFacade() {


super(Curso.class);


}

}


DisciplinaFacade.java

package fachada;


import javax.persistence.EntityManager;

import modelo.Disciplina;


public class DisciplinaFacade exte
nds AbstractFacade<Disciplina> {


private EntityManager em = AbstractFacade.emf.createEntityManager();



protected EntityManager getEntityManager() {


return em;


}



public DisciplinaFacade() {


super(Disciplina.class);


}

}

8)

A
gora, vamos criar os Beans Gerenciados que vão receber as requisições das páginas
Web da aplicação. Vamos criar 4 beans gerenciados dentro do pacote beansgerenciados:
PessoaManagedBean, AlunoManagedBean, CursoManagedBean e DisciplinaManagedBean. Os
códigos

desses beans gerenciados seguem abaixo:


PessoaManagedBean.java

package beansgerenciados;


import java.util.ArrayList;

import java.util.List;

import javax.faces.model.SelectItem;

import javax.faces.event.ActionEvent;

import javax.faces.bean.ManagedBean;

i
mport javax.faces.bean.RequestScoped;

import fachada.PessoaFacade;

import modelo.Pessoa;


@ManagedBean(name="PessoaManagedBean")

@RequestScoped

public class PessoaManagedBean {



private Pessoa pessoa = new Pessoa();


private PessoaFacade pessoaFacade
= new PessoaFacade();



public PessoaManagedBean() {


}



public Pessoa getPessoa() {


return pessoa;


}



public void setPessoa(Pessoa pessoa) {


this.pessoa = pessoa;


}



public String newPessoa() {


this.pessoaFacade.create
(this.pessoa);


return "newSuccess";


}



public String editPessoa() {


this.pessoaFacade.edit(this.pessoa);


return "editSuccess";


}



public void removePessoa(ActionEvent e) {


String id = (String) e.getComponent().getAttribu
tes().get("codPessoa");


this.pessoaFacade.remove(this.pessoaFacade.find(id));


}



public void findPessoa(ActionEvent e) {


String id = (String) e.getComponent().getAttributes().get("codPessoa");


this.pessoa = this.pessoaFacade.find(id
);


}



public List<Pessoa> getListaPessoas() {


return this.pessoaFacade.findAll();


}



public List<SelectItem> getPessoas() {


List<SelectItem> list = new ArrayList<SelectItem>();


List<Pessoa> it = getListaPessoas();


for (i
nt i = 0; i < it.size(); i++) {


Pessoa pessoa = it.get(i);


list.add(new SelectItem(pessoa, pessoa.getCpf()+""));


}


return list;


}

}


AlunoManagedBean.java

package beansgerenciados;


import java.util.ArrayList;

import java.
util.List;

import javax.faces.model.SelectItem;

import javax.faces.event.ActionEvent;

import javax.faces.bean.ManagedBean;

import javax.faces.bean.RequestScoped;

import fachada.AlunoFacade;

import modelo.Aluno;


@ManagedBean(name="AlunoManagedBean")

@Reque
stScoped

public class AlunoManagedBean {



private Aluno aluno = new Aluno();


private AlunoFacade alunoFacade = new AlunoFacade();



public AlunoManagedBean() {


}



public Aluno getAluno() {


return aluno;


}



public void setAluno(Alu
no aluno) {


this.aluno = aluno;


}



public String newAluno() {


this.alunoFacade.create(this.aluno);


return "newSuccess";


}



public String editAluno() {


this.alunoFacade.edit(this.aluno);


return "editSuccess";


}



public void removeAluno(ActionEvent e) {


String id = (String) e.getComponent().getAttributes().get("codAluno");


this.alunoFacade.remove(this.alunoFacade.find(id));


}



public void findAluno(ActionEvent e) {


String id = (String) e.
getComponent().getAttributes().get("codAluno");


this.aluno = this.alunoFacade.find(id);


}



public List<Aluno> getListaAlunos() {


return this.alunoFacade.findAll();


}



public List<SelectItem> getAlunos() {


List<SelectItem> list

= new ArrayList<SelectItem>();


List<Aluno> it = getListaAlunos();


for (int i = 0; i < it.size(); i++) {


Aluno aluno = it.get(i);


list.add(new SelectItem(aluno, aluno.getCpf()+""));


}


return list;


}

}


CursoMan
agedBean.java

package beansgerenciados;


import java.util.ArrayList;

import java.util.List;

import javax.faces.model.SelectItem;

import javax.faces.event.ActionEvent;

import javax.faces.bean.ManagedBean;

import javax.faces.bean.RequestScoped;

import fachad
a.CursoFacade;

import modelo.Curso;


@ManagedBean(name="CursoManagedBean")

@RequestScoped

public class CursoManagedBean {



private Curso curso = new Curso();


private CursoFacade cursoFacade = new CursoFacade();



public CursoManagedBean() {


}




public Curso getCurso() {


return curso;


}



public void setCurso(Curso curso) {


this.curso = curso;


}



public String newCurso() {


this.cursoFacade.create(this.curso);


return "newSuccess";


}



public String editCurs
o() {


this.cursoFacade.edit(this.curso);


return "editSuccess";


}



public void removeCurso(ActionEvent e) {


Integer id = (Integer) e.getComponent().getAttributes().get("codCurso");


this.cursoFacade.remove(this.cursoFacade.find(
id));


}



public void findCurso(ActionEvent e) {


Integer id = (Integer) e.getComponent().getAttributes().get("codCurso");


this.curso = this.cursoFacade.find(id);


}



public List<Curso> getListaCursos() {


return this.cursoFacade.
findAll();


}



public List<SelectItem> getCursos() {


List<SelectItem> list = new ArrayList<SelectItem>();


List<Curso> it = getListaCursos();


for (int i = 0; i < it.size(); i++) {


Curso curso = it.get(i);


list.add(ne
w SelectItem(curso, curso.getId()+""));


}


return list;


}

}


DisciplinaManagedBean.java

package beansgerenciados;


import java.util.ArrayList;

import java.util.List;

import javax.faces.model.SelectItem;

import javax.faces.event.ActionEvent;

import javax.faces.bean.ManagedBean;

import javax.faces.bean.RequestScoped;

import fachada.DisciplinaFacade;

import modelo.Disciplina;


@ManagedBean(name="DisciplinaManagedBean")

@RequestScoped

public class DisciplinaManagedBean {



private Disciplina di
sciplina = new Disciplina();


private DisciplinaFacade disciplinaFacade = new DisciplinaFacade();



public DisciplinaManagedBean() {


}



public Disciplina getDisciplina() {


return disciplina;


}



public void setDisciplina(Disciplina dis
ciplina) {


this.disciplina = disciplina;


}



public String newDisciplina() {


this.disciplinaFacade.create(this.disciplina);


return "newSuccess";


}



public String editDisciplina() {


this.disciplinaFacade.edit(this.discipli
na);


return "editSuccess";


}



public void removeDisciplina(ActionEvent e) {


Integer id = (Integer) e.getComponent().getAttributes().get("codDisciplina");


this.disciplinaFacade.remove(this.disciplinaFacade.find(id));


}



public
void findDisciplina(ActionEvent e) {


Integer id = (Integer) e.getComponent().getAttributes().get("codDisciplina");


this.disciplina = this.disciplinaFacade.find(id);


}



public List<Disciplina> getListaDisciplinas() {


return this.disc
iplinaFacade.findAll();


}



public List<SelectItem> getDisciplinas() {


List<SelectItem> list = new ArrayList<SelectItem>();


List<Disciplina> it = getListaDisciplinas();


for (int i = 0; i < it.size(); i++) {


Disciplina discip
lina = it.get(i);


list.add(new SelectItem(disciplina, disciplina.getId()+""));


}


return list;


}

}


9)

Agora, vamos criar uma classe Converter para a classe Curso. Isso é necessário, pois na
interface Web haverá um combobox para seleci
onar um curso no cadastro de Aluno e no
cadastro de Disciplina. Essa classe converter facilita a conversão do texto escrito no
combobox para o objeto referete aquele texto. Desta forma, vamos criar a classe
CursoConverter dentro do pacote converters.


Curs
oConverter.java

package converters;


import javax.faces.component.UIComponent;

import javax.faces.context.FacesContext;

import javax.faces.convert.Converter;

import javax.faces.convert.FacesConverter;

import fachada.CursoFacade;

import modelo.Curso;


@Face
sConverter("CursoConverter")

public class CursoConverter implements Converter {



public Object getAsObject(FacesContext context, UIComponent component, String value) {


return new CursoFacade().find(new Integer(value));


}



public String
getAsString(FacesContext context, UIComponent component, Object value) {


Curso curso = (Curso) value;


return curso.getId()+"";


}

}


10)


Agora vamos criar as páginas Web utilizando o framework JSF. Para cada uma das
classes do pacote modelo

(Pessoa, Aluno, Curso, Disciplina) deverão ser criadas 3 páginas
Web (cadastrar, editar e listar) e uma página index (página inicial).

11)


Dentro da pasta WebContent crie 4 novas pastas: Pessoa, Aluno, Curso e Disciplina.
Para criar uma nova pasta dentro da
pasta WebContent, clique com o botão direito do
mouse em cima da pasta WebContent, vá em New > Folder. Digite o nome da nova pasta e
clique em OK.

12)


Criada as 4 pastas, vamos criar as páginas Web referente a Pessoa, dentro da pasta
Pessoa. Para criar uma no
va página Web vá em New > Other.. E procure por JSP File.



newPessoa.jsp

<%@
page

contentType
=
"text/html"

pageEncoding
=
"UTF
-
8"
%>

<%@
taglib

prefix
=
"f"

uri
=
"http://java.sun.com/jsf/core"
%>

<%@
taglib

prefix
=
"h"

uri
=
"http://java.sun.com/jsf/html"
%>


<
f:view
>


<
html
>



<
head
>




<
meta

http
-
equiv
=
"Content
-
Type"

content
=
"text/html; charset=UTF
-
8"
/>




<
title
>
New Pessoa
</
title
>



</
head
>



<
body
>




<
div

class
=
"line
-
separator"
></
div
>




<
div

style
="
text
-
align
:
center
"
>




<
h1

class
=
"title"
>
New Pessoa
</
h1
>




</
div
>




<
div

class
=
"line
-
separator"
></
div
>




<
br
>




<
h:form
>





<
h:panelGrid

columns
=
"2"

styleClass
=
"table"
>






<
h:outputText

value
=
"cpf"
/>






<
h:inputText

required
=
"true"

label
=
"cpf"

value
=
"
#{PessoaManagedBean.pessoa.cpf}
"
/>






<
h:outputText

value
=
"n
ome"
/>






<
h:inputText

required
=
"true"

label
=
"nome"

value
=
"
#{PessoaManagedBean.pessoa.nome}
"
/>






<
h:outputText

value
=
"dataNascimento"
/>






<
h:inputText

required
=
"true"

label
=
"dataNascimento"

value
=
"
#{PessoaManagedBean.pessoa.dataNascimento}
"
>







<
f:convertDateTime

pattern
=
"dd/MM/yyyy"
/>






</
h:inputText
>






<
h:outputText

value
=
"telefone"
/>






<
h:inputText

label
=
"telefone"

value
=
"
#{PessoaManagedBean.pessoa.telefone}
"
/>





</
h:panelGrid
>





<
br
>





<
h:messages

errorClass
=
"error"
></
h:message
s
>





<
div

style
="
text
-
align
:
center
"
>






<
h:commandButton

value
=
"Save"

action
=
"
#{PessoaManagedBean.newPessoa}
"

styleClass
=
"save"
/>






<
h:commandButton

value
=
"Cancel"

immediate
=
"true"

action
=
"listPessoa"

styleClass
=
"cancel"
/>





</
div
>




</
h:form
>



</
body
>


</
html
>

</
f:view
>


editPessoa.jsp

<%@
page

contentType
=
"text/html"

pageEncoding
=
"UTF
-
8"
%>

<%@
taglib

prefix
=
"f"

uri
=
"http://java.sun.com/jsf/core"
%>

<%@
taglib

prefix
=
"h"

uri
=
"http://java.sun.com/jsf/html"
%>


<
f:view
>


<
html
>



<
head
>




<
meta

http
-
e
quiv
=
"Content
-
Type"

content
=
"text/html; charset=UTF
-
8"
/>




<
title
>
Edit
Pessoa
</
title
>



</
head
>



<
body
>




<
div

class
=
"line
-
separator"
></
div
>




<
div

style
="
text
-
align
:
center
"
>





<
h1

class
=
"title"
>
Edit
Pessoa
</
h1
>




</
div
>




<
div

class
=
"line
-
separato
r"
></
div
>




<
br
>




<
h:form
>





<
table

class
=
"table"
>






<
tr
>







<
td
>
cpf
</
td
>







<
td
>








<
h:inputHidden

value
=
"
#{PessoaManagedBean.pessoa.cpf}
"
/>








<
h:inputText

disabled
=
"true"

value
=
"
#{PessoaManagedBean.pessoa.cpf}
"
/>







</
td
>






<
/
tr
>






<
tr
>







<
td
>
nome
</
td
>







<
td
>








<
h:inputText

required
=
"true"

label
=
"nome"

value
=
"
#{PessoaManagedBean.pessoa.nome}
"
/>







</
td
>






</
tr
>






<
tr
>







<
td
>
dataNascimento
</
td
>







<
td
>








<
h:inputText

required
=
"true"

label
=
"d
ataNascimento"

value
=
"
#{PessoaManagedBean.pessoa.dataNascimento}
"
>









<
f:convertDateTime

pattern
=
"dd/MM/yyyy"
/>








</
h:inputText
>







</
td
>






</
tr
>






<
tr
>







<
td
>
telefone
</
td
>







<
td
>








<
h:inputText

label
=
"telefone"

value
=
"
#{Pes
soaManagedBean.pessoa.telefone}
"
/>







</
td
>






</
tr
>





</
table
>





<
br
>





<
h:messages

errorClass
=
"error"
></
h:messages
>





<
div

style
="
text
-
align
:
center
"
>






<
h:commandButton

value
=
"Save"

action
=
"
#{PessoaManagedBean.editPessoa}
"

styleClass
=
"sav
e"
/>






<
h:commandButton

value
=
"Cancel"

immediate
=
"true"

action
=
"listPessoa"

styleClass
=
"cancel"
/>





</
div
>




</
h:form
>



</
body
>


</
html
>

</
f:view
>


listPessoa.jsp

<%@
page
contentType
=
"text/html"

pageEncoding
=
"UTF
-
8"
%>

<%@
taglib

prefix
=
"f"

uri
=
"http:
//java.sun.com/jsf/core"
%>

<%@
taglib

prefix
=
"h"

uri
=
"http://java.sun.com/jsf/html"
%>


<
f:view
>


<
html
>



<
head
>




<
meta

http
-
equiv
=
"Content
-
Type"

content
=
"text/html; charset=UTF
-
8"
/>




<
title
>
List of Pessoa
</
title
>



</
head
>



<
body
>




<
div

class
=
"line
-
separator"
></
div
>




<
div

style
="
text
-
align
:
center
"
>





<
h1

class
=
"title"
>
List of Pessoa
</
h1
>




</
div
>




<
div

class
=
"line
-
separator"
></
div
>




<
br
>




<
h:form
>





<
h:dataTable

value
=
"
#{PessoaManagedBean.listaPessoas}
"

var
=
"item"

styleClass
=
"list_table"

headerClass
=
"header"

rowClasses
=
"row1,row2"
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"cpf"
/>







</
f:facet
>







<
h:outputText

value
=
"
#{item.cpf}
"
/>






</
h:column
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"nome"
/>







</
f:facet
>







<
h:outputText

value
=
"
#{item.nome}
"
/>






</
h:column
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"dataNascimento"
/>







</
f:facet
>







<
h:outputText

value
=
"
#{item.data
Nascimento}
"
>








<
f:convertDateTime

pattern
=
"dd/MM/yyyy"
/>







</
h:outputText
>






</
h:column
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"telefone"
/>







</
f:facet
>







<
h:outputText

value
=
"
#{item.telefone}
"
/>






</
h:column
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"Editar"
/>







</
f:facet
>







<
h:commandLink

value
=
"editar"

type
=
"submit"

action
=
"editPessoa"

actionListener
=
"
#{PessoaManagedBean.findPessoa}
"

>








<
f:attribute

name
=
"codPessoa"

value
=
"
#{item.cpf}
"
/>







</
h:commandLink
>






</
h:column
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"Remover"
/>







</
f:facet
>







<
h:commandLink

value
=
"remover"

type
=
"submit"

actionListener
=
"
#{Pess
oaManagedBean.removePessoa}
"

>








<
f:attribute

name
=
"codPessoa"

value
=
"
#{item.cpf}
"
/>







</
h:commandLink
>






</
h:column
>





</
h:dataTable
>





<
br
>





<
div

style
="
text
-
align
:
center
"
>






<
h:commandButton

value
=
"Back to Index"

immediate
=
"true"

a
ction
=
"index"

styleClass
=
"button"
/>






<
h:commandButton

value
=
"New Pessoa"

immediate
=
"true"

action
=
"newPessoa"

styleClass
=
"button"
/>





</
div
>




</
h:form
>



</
body
>


</
html
>

</
f:view
>


13)

Vamos criar as páginas Web referente ao Aluno dentro da pasta Aluno
.


newAluno.jsp

<%@
page

contentType
=
"text/html"
pageEncoding
=
"UTF
-
8"
%>

<%@
taglib

prefix
=
"f"

uri
=
"http://java.sun.com/jsf/core"
%>

<%@
taglib

prefix
=
"h"

uri
=
"http://java.sun.com/jsf/html"
%>


<
f:view
>


<
html
>



<
head
>




<
meta

http
-
equiv
=
"Content
-
Type"

content
=
"text/html; charset=UTF
-
8"
/>




<
title
>
New Aluno
</
title
>



</
head
>



<
body
>




<
div

class
=
"line
-
separator"
></
div
>




<
div

style
="
text
-
align
:
center
"
>




<
h1

class
=
"title"
>
New Aluno
</
h1
>




</
div
>




<
div

class
=
"line
-
separator"
></
div
>




<
br
>




<
h:form
>





<
h:panelGrid

columns
=
"2"

styleClass
=
"table"
>






<
h:outputText

value
=
"cpf"
/>






<
h:inputText

required
=
"true"

label
=
"cpf"

value
=
"
#{AlunoManagedBean.aluno.cpf}
"
/>






<
h:outputText

value
=
"nome"
/>






<
h:inputText

required
=
"true"

label
=
"nome"

value
=
"
#{
AlunoManagedBean.aluno.nome}
"
/>






<
h:outputText

value
=
"dataNascimento"
/>






<
h:inputText

required
=
"true"

label
=
"dataNascimento"

value
=
"
#{AlunoManagedBean.aluno.dataNascimento}
"
>







<
f:convertDateTime

pattern
=
"dd/MM/yyyy"
/>






</
h:inputText
>






<
h:outputText

value
=
"telefone"
/>






<
h:inputText

label
=
"telefone"

value
=
"
#{AlunoManagedBean.aluno.telefone}
"
/>






<
h:outputText

value
=
"ra"
/>






<
h:inputText

required
=
"true"

label
=
"ra"

value
=
"
#{AlunoManagedBean.aluno.ra}
"
/>






<
h:outputText

value
=
"e
mail"
/>






<
h:inputText

required
=
"true"

label
=
"email"

value
=
"
#{AlunoManagedBean.aluno.email}
"
/>






<
h:outputText

value
=
"escolaridade"
/>






<
h:inputText

label
=
"escolaridade"

value
=
"
#{AlunoManagedBean.aluno.escolaridade}
"
/>






<
h:outputText

value
=
"cu
rso"
/>






<
h:selectOneMenu

required
=
"true"

label
=
"curso"

value
=
"
#{AlunoManagedBean.aluno.curso}
"
>






<
f:selectItems

value
=
"
#{CursoManagedBean.cursos}
"

/>







<
f:converter

converterId
=
"CursoConverter"
/>






</
h:selectOneMenu
>





</
h:panelGrid
>





<
br
>





<
h:messages

errorClass
=
"error"
></
h:messages
>





<
div

style
="
text
-
align
:
center
"
>






<
h:commandButton

value
=
"Save"

action
=
"
#{AlunoManagedBean.newAluno}
"

styleClass
=
"save"
/>






<
h:commandButton

value
=
"Cancel"

immediate
=
"true"

action
=
"listAluno"

s
tyleClass
=
"cancel"
/>





</
div
>




</
h:form
>



</
body
>


</
html
>

</
f:view
>



editAluno.jsp

<%@
page

contentType
=
"text/html"

pageEncoding
=
"UTF
-
8"
%>

<%@
taglib

prefix
=
"f"

uri
=
"http://java.sun.com/jsf/core"
%>

<%@
taglib

prefix
=
"h"

uri
=
"http://java.sun.com/jsf/htm
l"
%>


<
f:view
>


<
html
>



<
head
>




<
meta

http
-
equiv
=
"Content
-
Type"

content
=
"text/html; charset=UTF
-
8"
/>




<
title
>
Edit Aluno
</
title
>



</
head
>



<
body
>




<
div

class
=
"line
-
separator"
></
div
>




<
div

style
="
text
-
align
:
center
"
>





<
h1

class
=
"title"
>
Edit Alun
o
</
h1
>




</
div
>




<
div

class
=
"line
-
separator"
></
div
>




<
br
>




<
h:form
>





<
table

class
=
"table"
>






<
tr
>







<
td
>
cpf
</
td
>







<
td
>








<
h:inputHidden

value
=
"
#{AlunoManagedBean.aluno.cpf}
"
/>








<
h:inputText

disabled
=
"true"

value
=
"
#{AlunoMan
agedBean.aluno.cpf}
"
/>







</
td
>






</
tr
>






<
tr
>







<
td
>
nome
</
td
>







<
td
>








<
h:inputText

required
=
"true"

label
=
"nome"

value
=
"
#{AlunoManagedBean.aluno.nome}
"
/>







</
td
>






</
tr
>






<
tr
>







<
td
>
dataNascimento
</
td
>







<
td
>








<
h:inputText

required
=
"true"

label
=
"dataNascimento"

value
=
"
#{AlunoManagedBean.aluno.dataNascimento}
"
>









<
f:convertDateTime

pattern
=
"dd/MM/yyyy"
/>








</
h:inputText
>







</
td
>






</
tr
>






<
tr
>







<
td
>
telefone
</
td
>







<
td
>








<
h:in
putText

label
=
"telefone"

value
=
"
#{AlunoManagedBean.aluno.telefone}
"
/>







</
td
>






</
tr
>






<
tr
>







<
td
>
ra
</
td
>







<
td
>








<
h:inputText

required
=
"true"

label
=
"ra"

value
=
"
#{AlunoManagedBean.aluno.ra}
"
/>







</
td
>






</
tr
>






<
tr
>







<
td
>
email
</
td
>







<
td
>








<
h:inputText

required
=
"true"

label
=
"email"

value
=
"
#{AlunoManagedBean.aluno.email}
"
/>







</
td
>






</
tr
>






<
tr
>







<
td
>
escolaridade
</
td
>







<
td
>








<
h:inputText

label
=
"escolaridade"

value
=
"
#{AlunoManagedBea
n.aluno.escolaridade}
"
/>







</
td
>






</
tr
>






<
tr
>







<
td
>
curso
</
td
>







<
td
>








<
h:selectOneMenu

required
=
"true"

label
=
"curso"

value
=
"
#{AlunoManagedBean.aluno.curso}
"
>









<
f:selectItems

value
=
"
#{CursoManagedBean.cursos}
"

/>









<
f
:converter

converterId
=
"CursoConverter"
/>








</
h:selectOneMenu
>







</
td
>






</
tr
>





</
table
>





<
br
>





<
h:messages

errorClass
=
"error"
></
h:messages
>





<
div

style
="
text
-
align
:
center
"
>






<
h:commandButton

value
=
"Save"

action
=
"
#{AlunoManagedB
ean.editAluno}
"

styleClass
=
"save"
/>






<
h:commandButton

value
=
"Cancel"

immediate
=
"true"

action
=
"listAluno"

styleClass
=
"cancel"
/>





</
div
>




</
h:form
>



</
body
>


</
html
>

</
f:view
>


listAluno.jsp

<%@
page

contentType
=
"text/html"

pageEncoding
=
"UTF
-
8"
%>

<%
@
taglib

prefix
=
"f"

uri
=
"http://java.sun.com/jsf/core"
%>

<%@
taglib

prefix
=
"h"

uri
=
"http://java.sun.com/jsf/html"
%>


<
f:view
>


<
html
>



<
head
>




<
meta

http
-
equiv
=
"Content
-
Type"

content
=
"text/html; charset=UTF
-
8"
/>




<
title
>
List of Aluno
</
title
>



</
head
>



<
body
>




<
div

class
=
"line
-
separator"
></
div
>




<
div

style
="
text
-
align
:
center
"
>





<
h1

class
=
"title"
>
List of Aluno
</
h1
>




</
div
>




<
div

class
=
"line
-
separator"
></
div
>




<
br
>




<
h:form
>





<
h:dataTable

value
=
"
#{AlunoManagedBean.listaAlunos}
"

var
=
"item
"

styleClass
=
"list_table"

headerClass
=
"header"

rowClasses
=
"row1,row2"
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"cpf"
/>







</
f:facet
>







<
h:outputText

value
=
"
#{item.cpf}
"
/>






</
h:column
>






<
h:column
>







<
f:fa
cet

name
=
"header"

>








<
h:outputText

value
=
"nome"
/>







</
f:facet
>







<
h:outputText

value
=
"
#{item.nome}
"
/>






</
h:column
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"dataNascimento"
/>







</
f:facet
>







<
h:outp
utText

value
=
"
#{item.dataNascimento}
"
>








<
f:convertDateTime

pattern
=
"dd/MM/yyyy"
/>







</
h:outputText
>






</
h:column
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"telefone"
/>







</
f:facet
>







<
h:outputText

value
=
"
#{item.telefone}
"
/>






</
h:column
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"ra"
/>







</
f:facet
>







<
h:outputText

value
=
"
#{item.ra}
"
/>






</
h:column
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outp
utText

value
=
"email"
/>







</
f:facet
>







<
h:outputText

value
=
"
#{item.email}
"
/>






</
h:column
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"escolaridade"
/>







</
f:facet
>







<
h:outputText

value
=
"
#{item.escolaridade}
"
/>






</
h:column
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"curso"
/>







</
f:facet
>







<
h:outputText

value
=
"
#{item.curso}
"
>








<
f:converter

converterId
=
"CursoConverter"
/>







</
h:outputText
>






</
h:column
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"Editar"
/>







</
f:facet
>







<
h:commandLink

value
=
"editar"

type
=
"submit"

action
=
"editAluno"

actionListener
=
"
#{AlunoManagedBean.findAluno}
"

>








<
f:attribute

name
=
"codAluno"

v
alue
=
"
#{item.cpf}
"
/>







</
h:commandLink
>






</
h:column
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"Remover"
/>







</
f:facet
>







<
h:commandLink

value
=
"remover"

type
=
"submit"

actionListener
=
"
#{AlunoManagedBean.remove
Aluno}
"

>








<
f:attribute

name
=
"codAluno"

value
=
"
#{item.cpf}
"
/>







</
h:commandLink
>






</
h:column
>





</
h:dataTable
>





<
br
>





<
div

style
="
text
-
align
:
center
"
>






<
h:commandButton

value
=
"Back to Index"

immediate
=
"true"

action
=
"index"

styleCla
ss
=
"button"
/>






<
h:commandButton

value
=
"New Aluno"

immediate
=
"true"

action
=
"newAluno"

styleClass
=
"button"
/>





</
div
>




</
h:form
>



</
body
>


</
html
>

</
f:view
>


14)

Vamos criar as páginas Web referente ao Curso dentro da pasta Curso.


newCurso.jsp

<%@
page

contentType
=
"text/html"
pageEncoding
=
"UTF
-
8"
%>

<%@
taglib

prefix
=
"f"

uri
=
"http://java.sun.com/jsf/core"
%>

<%@
taglib

prefix
=
"h"

uri
=
"http://java.sun.com/jsf/html"
%>


<
f:view
>


<
html
>



<
head
>




<
meta

http
-
equiv
=
"Content
-
Type"

content
=
"text/html; charset=UT
F
-
8"
/>




<
title
>
New
Curso
</
title
>



</
head
>



<
body
>




<
div

class
=
"line
-
separator"
></
div
>




<
div

style
="
text
-
align
:
center
"
>




<
h1

class
=
"title"
>
New
Curso
</
h1
>




</
div
>




<
div

class
=
"line
-
separator"
></
div
>




<
br
>




<
h:form
>





<
h:panelGrid

columns
=
"2"

styleClass
=
"table"
>






<
h:outputText

value
=
"nome"
/>






<
h:inputText

required
=
"true"

label
=
"nome"

value
=
"
#{CursoManagedBean.curso.nome}
"
/>






<
h:outputText

value
=
"nomeDepartamento"
/>






<
h:inputText

required
=
"true"

label
=
"nomeDepartamento"

value
=
"
#{CursoManagedBean.curso.nomeDepartamento}
"
/>






<
h:outputText

value
=
"cargaHoraria"
/>






<
h:inputText

label
=
"cargaHoraria"

value
=
"
#{CursoManagedBean.curso.cargaHoraria}
"
/>





</
h:panelGrid
>





<
br
>





<
h:messages

errorClass
=
"error"
></
h:messages
>





<
div

style
="
text
-
align
:
center
"
>






<
h:commandButton

value
=
"Save"

action
=
"
#{CursoManagedBean.newCurso}
"

styleClass
=
"save"
/>






<
h:commandButton

value
=
"Cancel"

immediate
=
"true"

action
=
"listCurso"

styleClass
=
"cancel"
/>





</
div
>




</
h:form
>



</
body
>


</
html
>

</
f:view
>



editCurso.jsp

<%@
page

contentType
=
"text/html"

pageEncoding
=
"UTF
-
8"
%>

<%@
taglib

prefix
=
"f"

uri
=
"http://java.sun.com/jsf/core"
%>

<%@
taglib

prefix
=
"h"

uri
=
"http://java.sun.com/jsf/html"
%>


<
f:view
>


<
html
>



<
head
>




<
meta

http
-
equiv
=
"C
ontent
-
Type"

content
=
"text/html; charset=UTF
-
8"
/>




<
title
>
Edit Curso
</
title
>



</
head
>



<
body
>




<
div

class
=
"line
-
separator"
></
div
>




<
div

style
="
text
-
align
:
center
"
>





<
h1

class
=
"title"
>
Edit Curso
</
h1
>




</
div
>




<
div

class
=
"line
-
separator"
></
div
>




<
br
>




<
h:form
>





<
table

class
=
"table"
>






<
h:inputHidden

value
=
"
#{CursoManagedBean.curso.id}
"
/>






<
tr
>







<
td
>
nome
</
td
>







<
td
>








<
h:inputText

required
=
"true"

label
=
"nome"

value
=
"
#{CursoManagedBean.curso.nome}
"
/>







</
td
>






</
tr
>






<
tr
>







<
td
>
nomeDepartamento
</
td
>







<
td
>








<
h:inputText

required
=
"true"

label
=
"nomeDepartamento"

value
=
"
#{CursoManagedBean.curso.nomeDepartamento}
"
/>







</
td
>






</
tr
>






<
tr
>







<
td
>
cargaHoraria
</
td
>







<
td
>








<
h:inp
utText

label
=
"cargaHoraria"

value
=
"
#{CursoManagedBean.curso.cargaHoraria}
"
/>







</
td
>






</
tr
>





</
table
>





<
br
>





<
h:messages

errorClass
=
"error"
></
h:messages
>





<
div

style
="
text
-
align
:
center
"
>






<
h:commandButton

value
=
"Save"

action
=
"
#{Curs
oManagedBean.editCurso}
"

styleClass
=
"save"
/>






<
h:commandButton

value
=
"Cancel"

immediate
=
"true"

action
=
"listCurso"

styleClass
=
"cancel"
/>





</
div
>




</
h:form
>



</
body
>


</
html
>

</
f:view
>


listCurso.jsp

<%@
page

contentType
=
"text/html"

pageEncoding
=
"UT
F
-
8"
%>

<%@
taglib

prefix
=
"f"

uri
=
"http://java.sun.com/jsf/core"
%>

<%@
taglib

prefix
=
"h"

uri
=
"http://java.sun.com/jsf/html"
%>


<
f:view
>


<
html
>



<
head
>




<
meta

http
-
equiv
=
"Content
-
Type"

content
=
"text/html; charset=UTF
-
8"
/>




<
title
>
List of
Curso
</
title
>



</
head
>



<
body
>




<
div

class
=
"line
-
separator"
></
div
>




<
div

style
="
text
-
align
:
center
"
>





<
h1

class
=
"title"
>
List of
Curso
</
h1
>




</
div
>




<
div

class
=
"line
-
separator"
></
div
>




<
br
>




<
h:form
>





<
h:dataTable

value
=
"
#{CursoManagedBean.listaCursos}
"

var
=
"item"

styleClass
=
"list_table"

headerClass
=
"header"

rowClasses
=
"row1,row2"
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"id"
/>







</
f:facet
>







<
h:outputText

value
=
"
#{item.id}
"
/>






</
h:column
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"nome"
/>







</
f:facet
>







<
h:outputText

value
=
"
#{item.nome}
"
/>






</
h:column
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"nomeDepartamento"
/>







</
f:facet
>







<
h:outputText

value
=
"
#{item.nomeDepartamento}
"
/>






</
h:column
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"cargaHoraria"
/>







</
f:facet
>







<
h:outputText

value
=
"
#{item.cargaHoraria}
"
/>






</
h:column
>






<
h:colu
mn
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"Editar"
/>







</
f:facet
>







<
h:commandLink

value
=
"editar"

type
=
"submit"

action
=
"editCurso"

actionListener
=
"
#{CursoManagedBean.findCurso}
"

>








<
f:attribute

name
=
"codCurso"

value
=
"
#{ite
m.id}
"
/>







</
h:commandLink
>






</
h:column
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"Remover"
/>







</
f:facet
>







<
h:commandLink

value
=
"remover"

type
=
"submit"

actionListener
=
"
#{CursoManagedBean.removeCurso}
"

>








<
f:attribute

name
=
"codCurso"

value
=
"
#{item.id}
"
/>







</
h:commandLink
>






</
h:column
>





</
h:dataTable
>





<
br
>





<
div

style
="
text
-
align
:
center
"
>






<
h:commandButton

value
=
"Back to Index"

immediate
=
"true"

action
=
"index"

styleClass
=
"button"
/>






<
h:commandButton

value
=
"New Curso"

immediate
=
"true"

action
=
"newCurso"

styleClass
=
"button"
/>





</
div
>




</
h:form
>



</
body
>


</
html
>

</
f:view
>



15)


Vamos criar as páginas Web referente a Disciplina dentro da pasta Disciplina.


newDisciplina.jsp

<%@
p
age

c ontentType
=
"text/html"

p ageEncoding
=
"UTF
-
8"
%>

<%@
taglib

prefix
=
"f"

uri
=
"http://java.sun.com/jsf/core"
%>

<%@
taglib

prefix
=
"h"

uri
=
"http://java.sun.com/jsf/html"
%>


<
f:view
>


<
html
>



<
head
>




<
meta

http
-
equiv
=
"Content
-
Type"

content
=
"text/html; charset
=UTF
-
8"
/>




<
title
>
New Disciplina
</
title
>



</
head
>



<
body
>




<
div

class
=
"line
-
separator"
></
div
>




<
div

style
="
text
-
align
:
center
"
>




<
h1

class
=
"title"
>
New Disciplina
</
h1
>




</
div
>




<
div

class
=
"line
-
separator"
></
div
>




<
br
>




<
h:form
>





<
h:panel
Grid

columns
=
"2"

styleClass
=
"table"
>






<
h:outputText

value
=
"nome"
/>






<
h:inputText

required
=
"true"

label
=
"nome"

value
=
"
#{DisciplinaManagedBean.disciplina.nome}
"
/>






<
h:outputText

value
=
"quantidadeCreditos"
/>






<
h:inputText

label
=
"quantidadeCred
itos"

value
=
"
#{DisciplinaManagedBean.disciplina.quantidadeCreditos}
"
/>






<
h:outputText

value
=
"nomeProfessor"
/>






<
h:inputText

label
=
"nomeProfessor"

value
=
"
#{DisciplinaManagedBean.disciplina.nomeProfessor}
"
/>






<
h:outputText

value
=
"curso"
/>






<
h
:selectOneMenu

required
=
"true"

label
=
"curso"

value
=
"
#{DisciplinaManagedBean.disciplina.curso}
"
>






<
f:selectItems

value
=
"
#{CursoManagedBean.cursos}
"

/>







<
f:converter

converterId
=
"CursoConverter"
/>






</
h:selectOneMenu
>





</
h:panelGrid
>





<
br
>





<
h:messages

errorClass
=
"error"
></
h:messages
>





<
div

style
="
text
-
align
:
center
"
>






<
h:commandButton

value
=
"Save"

action
=
"
#{DisciplinaManagedBean.newDisciplina}
"

styleClass
=
"save"
/>






<
h:commandButton

value
=
"Cancel"

immediate
=
"true"

action
=
"listDi
sciplina"

styleClass
=
"cancel"
/>





</
div
>




</
h:form
>



</
body
>


</
html
>

</
f:view
>


editDisciplina.jsp

<%@
page

c ontentType
=
"text/html"

p ageEncoding
=
"UTF
-
8"
%>

<%@
taglib

prefix
=
"f"

uri
=
"http://java.sun.com/jsf/core"
%>

<%@
taglib

prefix
=
"h"

uri
=
"http://java.
sun.com/jsf/html"
%>


<
f:view
>


<
html
>



<
head
>




<
meta

http
-
equiv
=
"Content
-
Type"

content
=
"text/html; charset=UTF
-
8"
/>




<
title
>
Edit Disciplina
</
title
>



</
head
>



<
body
>




<
div

class
=
"line
-
separator"
></
div
>




<
div

style
="
text
-
align
:
center
"
>





<
h1

cla
ss
=
"title"
>
Edit Disciplina
</
h1
>




</
div
>




<
div

class
=
"line
-
separator"
></
div
>




<
br
>




<
h:form
>





<
table

class
=
"table"
>






<
h:inputHidden

value
=
"
#{DisciplinaManagedBean.disciplina.id}
"
/>






<
tr
>







<
td
>
nome
</
td
>







<
td
>








<
h:inputText

required
=
"true"

label
=
"nome"

value
=
"
#{DisciplinaManagedBean.disciplina.nome}
"
/>







</
td
>






</
tr
>






<
tr
>







<
td
>
quantidadeCreditos
</
td
>







<
td
>








<
h:inputText

label
=
"quantidadeCreditos"

value
=
"
#{DisciplinaManagedBean.disciplina.quantidad
eCreditos}
"
/>







</
td
>






</
tr
>






<
tr
>







<
td
>
nomeProfessor
</
td
>







<
td
>








<
h:inputText

label
=
"nomeProfessor"

value
=
"
#{DisciplinaManagedBean.disciplina.nomeProfessor}
"
/>







</
td
>






</
tr
>






<
tr
>







<
td
>
curso
</
td
>







<
td
>








<
h:selectOneMenu

required
=
"true"

label
=
"curso"

value
=
"
#{DisciplinaManagedBean.disciplina.curso}
"
>









<
f:selectItems

value
=
"
#{CursoManagedBean.cursos}
"

/>









<
f:converter

converterId
=
"CursoConverter"
/>








</
h:selectOneMenu
>







</
td
>






</
tr
>





</
table
>





<
br
>





<
h:messages

errorClass
=
"error"
></
h:messages
>





<
div

style
="
text
-
align
:
center
"
>






<
h:commandButton

value
=
"Save"

action
=
"
#{DisciplinaManagedBean.editDisciplina}
"

styleClass
=
"save"
/>






<
h:commandButton

value
=
"Cancel"

immediate
=
"true"

action
=
"listDisciplina"

styleClass
=
"cancel"
/>





</
div
>




</
h:form
>



</
body
>


</
html
>

</
f:view
>


listDisciplina.jsp

<%@
page

c ontentType
=
"text/html"

p ageEncoding
=
"UTF
-
8"
%>

<%@
taglib

prefix
=
"f"

uri
=
"http://java.sun.com/jsf/core"
%>

<%@
tag
lib

prefix
=
"h"

uri
=
"http://java.sun.com/jsf/html"
%>


<
f:view
>


<
html
>



<
head
>




<
meta

http
-
equiv
=
"Content
-
Type"

content
=
"text/html; charset=UTF
-
8"
/>




<
title
>
List of Disciplina
</
title
>



</
head
>



<
body
>




<
div

class
=
"line
-
separator"
></
div
>




<
div

sty
le
="
text
-
align
:
center
"
>





<
h1

class
=
"title"
>
List of Disciplina
</
h1
>




</
div
>




<
div

class
=
"line
-
separator"
></
div
>




<
br
>




<
h:form
>





<
h:dataTable

value
=
"
#{DisciplinaManagedBean.listaDisciplinas}
"

var
=
"item"

styleClass
=
"list_table"

headerClass
=
"hea
der"

rowClasses
=
"row1,row2"
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"id"
/>







</
f:facet
>







<
h:outputText

value
=
"
#{item.id}
"
/>






</
h:column
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

val
ue
=
"nome"
/>







</
f:facet
>







<
h:outputText

value
=
"
#{item.nome}
"
/>






</
h:column
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"quantidadeCreditos"
/>







</
f:facet
>







<
h:outputText

value
=
"
#{item.quantidadeCreditos}
"
/>






</
h:column
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"nomeProfessor"
/>







</
f:facet
>







<
h:outputText

value
=
"
#{item.nomeProfessor}
"
/>






</
h:column
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:
outputText

value
=
"curso"
/>







</
f:facet
>







<
h:outputText

value
=
"
#{item.curso}
"
>








<
f:converter

converterId
=
"CursoConverter"
/>







</
h:outputText
>






</
h:column
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"Edit
ar"
/>







</
f:facet
>







<
h:commandLink

value
=
"editar"

type
=
"submit"

action
=
"editDisciplina"

actionListener
=
"
#{DisciplinaManagedBean.findDisciplina}
"

>








<
f:attribute

name
=
"codDisciplina"

value
=
"
#{item.id}
"
/>







</
h:commandLink
>






</
h:column
>






<
h:column
>







<
f:facet

name
=
"header"

>








<
h:outputText

value
=
"Remover"
/>







</
f:facet
>







<
h:commandLink

value
=
"remover"

type
=
"submit"

actionListener
=
"
#{DisciplinaManagedBean.removeDisciplina}
"

>








<
f:attribute

name
=
"codDisciplina
"

value
=
"
#{item.id}
"
/>







</
h:commandLink
>






</
h:column
>





</
h:dataTable
>





<
br
>





<
div

style
="
text
-
align
:
center
"
>






<
h:commandButton

value
=
"Back to Index"

immediate
=
"true"

action
=
"index"

styleClass
=
"button"
/>






<
h:commandButton

value
=
"Ne
w Disciplina"

immediate
=
"true"

action
=
"newDisciplina"

styleClass
=
"button"
/>





</
div
>




</
h:form
>



</
body
>


</
html
>

</
f:view
>


16)


Vamos criar a página Web de index dentro da pasta WebContent.


index.jsp

<%@
page

c ontentType
=
"text/html"

p ageEncoding
=
"UTF
-
8
"
%>


<%@
taglib

prefix
=
"f"

uri
=
"http://java.sun.com/jsf/core"
%>

<%@
taglib

prefix
=
"h"

uri
=
"http://java.sun.com/jsf/html"
%>

<
f:view
>


<
html
>



<
head
>




<
meta

http
-
equiv
=
"Content
-
Type"

content
=
"text/html; charset=UTF
-
8"
/>




<
title
>
index
</
title
>



</
head
>



<
body
>




<
div

class
=
"line
-
separator"
></
div
>




<
div

style
="
text
-
align
:
center
"
>





<
h1

class
=
"title"
>
CRUD
</
h1
>




</
div
>




<
div

class
=
"line
-
separator"
></
div
>




<
br
>




<
h:form
>





<
h:panelGrid

styleClass
=
"list_table"

rowClasses
=
"row1,row2"
>






<
h:comm
andLink

value
=
"Disciplina"

action
=
"listDisciplina"
/>






<
h:commandLink

value
=
"Pessoa"

action
=
"listPessoa"
/>






<
h:commandLink

value
=
"Aluno"

action
=
"listAluno"
/>






<
h:commandLink

value
=
"Curso"

action
=
"listCurso"
/>





</
h:panelGrid
>




</
h:form
>



</
body
>


</
html
>

</
f:view
>


17)


Vamos adicionar as regras de navegação no arquivo faces
-
config.xml que está na pasta
WebContent/WEB
-
INF. O código final do arquivo segue abaixo:

faces
-
config.xml

<?
xml

version
=
'1.0'

encoding
=
'UTF
-
8'
?>


<
faces
-
config

version
=
"2.0"


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"
>



<
converter
>


<
converter
-
id
>
CursoConverter
</
converter
-
id
>


<
converter
-
class
>
converters.CursoConverter
</
converter
-
class
>


</
converter
>


<
navigation
-
rule
>


<
from
-
view
-
id
>
*
</
from
-
view
-
id
>


<
navigation
-
case
>


<
from
-
outcome
>
newDisciplina
</
from
-
outcome
>


<
to
-
view
-
id
>
/
Disciplina
/newDisciplina.jsp
</
to
-
view
-
id
>



<
redirect
/>


</
navigation
-
case
>


<
navigation
-
case
>


<
from
-
outcome
>
editDisciplina
</
from
-
outcome
>


<
to
-
view
-
id
>
/
Disciplina
/editDisciplina.jsp
</
to
-
view
-
id
>


</
navigation
-
case
>


<
n
avigation
-
case
>


<
from
-
outcome
>
listDisciplina
</
from
-
outcome
>


<
to
-
view
-
id
>
/
Disciplina
/listDisciplina.jsp
</
to
-
view
-
id
>



<
redirect
/>


</
navigation
-
case
>


<
navigation
-
case
>


<
from
-
outcome
>
index
</
from
-
outcome
>


<
to
-
view
-
id
>
/i
ndex.jsp
</
to
-
view
-
id
>



<
redirect
/>


</
navigation
-
case
>

</
navigation
-
rule
>


<
navigation
-
rule
>


<
from
-
view
-
id
>
/
Disciplina
/newDisciplina.jsp
</
from
-
view
-
id
>


<
navigation
-
case
>


<
from
-
outcome
>
newSuccess
</
from
-
outcome
>


<
to
-
view
-
id
>
/
Disci
plina
/listDisciplina.jsp
</
to
-
view
-
id
>



<
redirect
/>


</
navigation
-
case
>

</
navigation
-
rule
>


<
navigation
-
rule
>


<
from
-
view
-
id
>
/
Disciplina
/editDisciplina.jsp
</
from
-
view
-
id
>


<
navigation
-
case
>


<
from
-
outcome
>
editSuccess
</
from
-
outcome
>


<
to
-
view
-
id
>
/
Disciplina
/listDisciplina.jsp
</
to
-
view
-
id
>



<
redirect
/>


</
navigation
-
case
>

</
navigation
-
rule
>


<
navigation
-
rule
>


<
from
-
view
-
id
>
*
</
from
-
view
-
id
>


<
navigation
-
case
>


<
from
-
outcome
>
newPessoa
</
from
-
outcome
>


<
to
-
view
-
id
>
/
Pessoa
/newPessoa.jsp
</
to
-
view
-
id
>



<
redirect
/>


</
navigation
-
case
>


<
navigation
-
case
>


<
from
-
outcome
>
editPessoa
</
from
-
outcome
>


<
to
-
view
-
id
>
/
Pessoa
/editPessoa.jsp
</
to
-
view
-
id
>


</
navigation
-
case
>


<
navigation
-
case
>


<
from
-
outcome
>
listPessoa
</
from
-
outcome
>


<
to
-
view
-
id
>
/
Pessoa
/listPessoa.jsp
</
to
-
view
-
id
>



<
redirect
/>


</
navigation
-
case
>


<
navigation
-
case
>


<
from
-
outcome
>
index
</
from
-
outcome
>


<
to
-
view
-
id
>
/index.jsp
</
to
-
view
-
id
>



<
redirect
/>


</
navigation
-
case
>

</
navigation
-
rule
>


<
navigation
-
rule
>


<
from
-
view
-
id
>
/
Pessoa
/newPessoa.jsp
</
from
-
view
-
id
>


<
navigation
-
case
>


<
from
-
outcome
>
newSuccess
</
from
-
outcome
>


<
to
-
view
-
id
>
/
Pessoa
/listPessoa.jsp
</
to
-
view
-
id
>



<
redirect
/>


</
navigation
-
case
>

</
navigation
-
rule
>


<
navigation
-
rule
>


<
from
-
view
-
id
>
/
Pessoa
/editPessoa.jsp
</
from
-
view
-
id
>


<
navigation
-
case
>


<
from
-
outcome
>
editSuccess
</
from
-
outcome
>


<
to
-
view
-
id
>
/
Pessoa
/listPessoa.jsp
</
to
-
view
-
id
>



<
redirect
/>


</
navigation
-
case
>

</
navigation
-
rule
>


<
navigation
-
rule
>


<
from
-
view
-
id
>
*
</
from
-
view
-
id
>


<
navigation
-
case
>


<
from
-
outcome
>
newAluno
</
from
-
outcome
>


<
to
-
view
-
id
>
/
Aluno
/newAluno.jsp
</
to
-
view
-
id
>



<
redirect
/>


</
navigation
-
case
>


<
n
avigation
-
case
>


<
from
-
outcome
>
editAluno
</
from
-
outcome
>


<
to
-
view
-
id
>
/
Aluno
/editAluno.jsp
</
to
-
view
-
id
>


</
navigation
-
case
>


<
navigation
-
case
>


<
from
-
outcome
>
listAluno
</
from
-
outcome
>


<
to
-
view
-
id
>
/
Aluno
/listAluno.jsp
</
to
-
vi
ew
-
id
>



<
redirect
/>


</
navigation
-
case
>


<
navigation
-
case
>


<
from
-
outcome
>
index
</
from
-
outcome
>


<
to
-
view
-
id
>
/index.jsp
</
to
-
view
-
id
>



<
redirect
/>


</
navigation
-
case
>

</
navigation
-
rule
>


<
navigation
-
rule
>


<
from
-
view
-
id
>
/
Aluno
/new
Aluno.jsp
</
from
-
view
-
id
>


<
navigation
-
case
>


<
from
-
outcome
>
newSuccess
</
from
-
outcome
>


<
to
-
view
-
id
>
/
Aluno
/listAluno.jsp
</
to
-
view
-
id
>



<
redirect
/>


</
navigation
-
case
>

</
navigation
-
rule
>


<
navigation
-
rule
>


<
from
-
view
-
id
>
/
Aluno
/editAlu
no.jsp
</
from
-
view
-
id
>


<
navigation
-
case
>


<
from
-
outcome
>
editSuccess
</
from
-
outcome
>


<
to
-
view
-
id
>
/
Aluno
/listAluno.jsp
</
to
-
view
-
id
>



<
redirect
/>


</
navigation
-
case
>

</
navigation
-
rule
>


<
navigation
-
rule
>


<
from
-
view
-
id
>
*
</
from
-
view
-
id
>


<
navigation
-
case
>


<
from
-
outcome
>
newCurso
</
from
-
outcome
>


<
to
-
view
-
id
>
/
Curso
/newCurso.jsp
</
to
-
view
-
id
>



<
redirect
/>


</
navigation
-
case
>


<
navigation
-
case
>


<
from
-
outcome
>
editCurso
</
from
-
outcome
>


<
to
-
view
-
id
>
/
Curso
/e
ditCurso.jsp
</
to
-
view
-
id
>


</
navigation
-
case
>


<
navigation
-
case
>


<
from
-
outcome
>
listCurso
</
from
-
outcome
>


<
to
-
view
-
id
>
/
Curso
/listCurso.jsp
</
to
-
view
-
id
>



<
redirect
/>


</
navigation
-
case
>


<
navigation
-
case
>


<
from
-
outcome
>
ind
ex
</
from
-
outcome
>


<
to
-
view
-
id
>
/index.jsp
</
to
-
view
-
id
>



<
redirect
/>


</
navigation
-
case
>

</
navigation
-
rule
>


<
navigation
-
rule
>


<
from
-
view
-
id
>
/
Curso
/newCurso.jsp
</
from
-
view
-
id
>


<
navigation
-
case
>


<
from
-
outcome
>
newSuccess
</
from
-
outco
me
>


<
to
-
view
-
id
>
/
Curso
/listCurso.jsp
</
to
-
view
-
id
>



<
redirect
/>


</
navigation
-
case
>

</
navigation
-
rule
>


<
navigation
-
rule
>


<
from
-
view
-
id
>
/
Curso
/editCurso.jsp
</
from
-
view
-
id
>


<
navigation
-
case
>


<
from
-
outcome
>
editSuccess
</
from
-
outcome
>


<
to
-
view
-
id
>
/
Curso
/listCurso.jsp
</
to
-
view
-
id
>



<
redirect
/>


</
navigation
-
case
>

</
navigation
-
rule
>


</
faces
-
config
>