Agora iremos criar a classe ClientController no pacote com.tutorialday.controller , a mesma será o nosso FacesBean.
CLASSE ClientController
package com.tutorialday.controller;
import java.util.List;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import com.tutorialday.dao.BaseDao;
import com.tutorialday.model.City;
import com.tutorialday.model.Client;
public class ClientController {
private Client client;
private City city;
private DataModel model;
private String ccity;
public String getCcity() {
return ccity;
}
public void setCcity(String ccity) {
this.ccity = ccity;
}
public City getCity() {
return city;
}
public void setCity(City city) {
this.city = city;
}
public Client getClient() {
return client;
}
public void setClient(Client client) {
this.client = client;
}
// METODO QUE CRIA UM OBJETO CLIENT
public String newClient(){
this.client = new Client();
return "newClient";
}
// METODO QUE MANIPULA A PERSISTÊNCIA DO OBJETO CLIENT
public String create(){
BaseDao<City> cdao = new BaseDao<City>();
City city = cdao.findCityByName(ccity); // FAZENDO CONSULTA PELO NOME DA CIDADE
setCity(city); // "SETANDO" A CIDADE
BaseDao<Client> dao = new BaseDao<Client>();
client.setCity(city);
dao.create(client);
return "success_creCli";
}
// METODO QUE MANIPULA A EXCLUSÃO DO OBJETO CLIENT
public String delete(){
BaseDao<Client> dao = new BaseDao<Client>();
Client client = (Client) model.getRowData();
dao.delete(client);
return "success_delCli";
}
// METODO QUE CATA O OBJETO CLIENT DA DATATABLE (PAGINA)
public String edit(){
Client client = (Client) model.getRowData();
setClient(client);
return "editCli";
}
// METODO QUE MANIPULA A ALTERAÇÃO DO OBJETO CLIENT
public String update(){
BaseDao<City> cdao = new BaseDao<City>();
City city = cdao.findCityByName(ccity);
setCity(city);
BaseDao<Client> dao = new BaseDao<Client>();
client.setCity(city);
dao.update(client);
return "success_upd";
}
// METODO QUE CRIA UM OBJETO CITY
public String newCity(){
this.city = new City();
return "newCity";
}
// METODO QUE MANIPULA A PERSISTÊNCIA DO OBJETO CITY
public String createCity(){
BaseDao<City> cdao = new BaseDao<City>();
cdao.create(city);
return "success_creCity";
}
// METODO QUE MANIPULA A EXCLUSÃO DO OBJETO CITY
public String deleteCity(){
BaseDao<City> dao = new BaseDao<City>();
City city = (City) model.getRowData();
dao.delete(city);
return "success_delCity";
}
// METODO QUE CATA O OBJETO CITY DA DATATABLE (PAGINA)
public String editCity(){
City city = (City) model.getRowData();
setCity(city);
return "editCity";
}
// METODO QUE MANIPULA A ALTERAÇÃO DO OBJETO CITY
public String updateCity(){
BaseDao<City> dao = new BaseDao<City>();
dao.update(city);
return "success_upd";
}
// METODO QUE RETORNA UMA LISTA DE CLIENT PARA O
// DATATABLE
public DataModel getAllClient(){
BaseDao<Client> dao = new BaseDao<Client>();
model = new ListDataModel(dao.allClient());
return model;
}
// METODO QUE RETORNA UMA LISTA DE CITY PARA O DATATABLE
public DataModel getAllCity(){
BaseDao<City> dao = new BaseDao<City>();
model = new ListDataModel(dao.allCity());
return model;
}
// METODO QUE RETORNA UMA LISTA COM OS NOMES DAS CITYS
//PARA O SELECTITEMS (PAGINA)
public List getListNameCity(){
BaseDao<SelectItem> dao = new BaseDao<SelectItem>();
return dao.listCity();
}
}
Devemos agora criar um o arquivo persistence.xml no diretório META-INF que criamos, este arquivo mapeará nossas classes models e o banco de dados.
ARQUIVO persistence.xml
<persistence 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 persistence_1_0.xsd"
version="1.0">
<persistence-unit name="tutorial" transaction-type="RESOURCE_LOCAL">
<provider>
oracle.toplink.essentials.PersistenceProvider
</provider>
<class>com.tutorialday.model.Client</class>
<class>com.tutorialday.model.City</class>
<properties>
<property name="toplink.logging.level" value="FINE" />
<property name="toplink.jdbc.driver" value="com.ibm.db2.jcc.DB2Driver" />
<property name="toplink.jdbc.url" value="jdbc:db2://localhost:50000/TUTORIAL" />
<property name="toplink.jdbc.password" value="SENHA AQUI" />
<property name="toplink.jdbc.user" value="USUARIO AQUI" />
</properties>
<!-- MySQL DataBase
<properties>
<property name="toplink.logging.level" value="FINE" />
<property name="toplink.jdbc.driver"
value="com.mysql.jdbc.Driver" />
<property name="toplink.jdbc.url"
value="jdbc:mysql://localhost:3306/TUTORIAL" />
<property name="toplink.jdbc.password" value="" />
<property name="toplink.jdbc.user" value="root" />
</properties>
-->
</persistence-unit>
</persistence>
Devemos observar que temos em comentário neste XML a configuração para o banco de dados MySQL(em fonte verde), não esqueça de colocar o usuário e a senha corretamente do seu banco de dados.
Para finalizarmos esta segunda etapa deste tutorial agora iremos mapear o nosso FacesBean e fazer a regra de navegação no arquivo faces-config.xml que se encontra dentro de ../WebContent/WEB-INF/ . Entre as tags <faces-config> </faces-config> , devemos adicionar as tags abaixo:
<!-- Bean ClientController -->
<managed-bean>
<managed-bean-name>clientView</managed-bean-name>
<managed-bean-class>
com.tutorialday.controller.ClientController
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<display-name>newCity</display-name>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>newCity</from-outcome>
<to-view-id>/formCity.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<display-name>newClient</display-name>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>newClient</from-outcome>
<to-view-id>/formClient.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<display-name>listCity</display-name>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>ListCity</from-outcome>
<to-view-id>/listCity.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<display-name>listClient</display-name>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>ListClient</from-outcome>
<to-view-id>/listClient.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<display-name>successCreCity</display-name>
<from-view-id>/formCity.jsp</from-view-id>
<navigation-case>
<from-outcome>success_creCity</from-outcome>
<to-view-id>/listCity.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<display-name>successDelCity</display-name>
<from-view-id>/listCity.jsp</from-view-id>
<navigation-case>
<from-outcome>success_delCity</from-outcome>
<to-view-id>/listCity.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<display-name>successCreClient</display-name>
<from-view-id>/formClient.jsp</from-view-id>
<navigation-case>
<from-outcome>success_creCli</from-outcome>
<to-view-id>/listClient.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<display-name>successDelClient</display-name>
<from-view-id>/listClient.jsp</from-view-id>
<navigation-case>
<from-outcome>success_delCli</from-outcome>
<to-view-id>/listClient.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<display-name>editClient</display-name>
<from-view-id>/listClient.jsp</from-view-id>
<navigation-case>
<from-outcome>editCli</from-outcome>
<to-view-id>/alterClient.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<display-name>successUpdClient</display-name>
<from-view-id>/alterClient.jsp</from-view-id>
<navigation-case>
<from-outcome>success_upd</from-outcome>
<to-view-id>/listClient.jsp</to-view-id>
</navigation-case>
</navigation-rule>
Pronto! Temos nosso projeto configurado, não podemos esquecer de colocar as seguintes JARS dentro do diretório ../WebContent/WEB-INF/lib/ :
· Db2jcc.jar
· Db2jcc_license_cu.jar
· Ejb3-persistence.jar
· Mysql-connector-java-5.0.X-bin.jar (Caso use o MySQL)
· Toplink-essentials.jar
· JARS para o JSF
Obs: As JARS do framework JSF geralmente são adicionados automaticamente pela IDE, no caso da IDE Eclipse-Rational Software Architect é adicionado apenas a JAR jsf-ibm.jar .
No proximo post iremos finalizar este tutorial com a criação das páginas JSPs(View).
Até maissss...............