shyricosai 2 de ago. de 2008
Vlw pela ajuda.
Segue abaixo o codigo.
Classe Cliente
<% @ taglib uri = "http://richfaces.org/a4j" prefix = "a4j" %>
<% @ taglib uri = "http://richfaces.org/rich" prefix = "rich" %>
<% @ taglib uri = "http://java.sun.com/jsf/core" prefix = "f" %>
<% @ taglib uri = "http://java.sun.com/jsf/html" prefix = "h" %>
<f:subview id= "CadCliente" >
<rich:tabPanel>
<rich:tab label= "Cadastrar Cliente" >
<h:form id= "cadastro" >
<rich:panel>
<f:facet name= "panelcadcli" >
Insira os Dados Pessoais do Cliente, campos marcados com * são obrigatorios.
</f:facet>
</rich:panel>
<rich:spacer height= "8" />
<rich:panel>
<table>
<tr>
<td><h:outputText value= "Nome Completo" /></td>
<td><h:inputText id= "nome" value= "#{clienteHandler.cliente.nome}" required= "true" >
<f:validateLength minimum= "4" />
</h:inputText>
<h:outputText value= " *" />
<h:message for= "nome" style= "color: darkred" /></td>
</tr>
<tr>
<td> Sexo:</td>
<td><h:selectOneRadio id= "sexo" value= "#{clienteHandler.cliente.sexo}" required= "true" >
<f:selectItem itemLabel= "Feminino" itemValue= "F" />
<f:selectItem itemLabel= "Masculino" itemValue= "M" />
</h:selectOneRadio>
<h:message for= "sexo" style= "color: darkred" /></td>
</tr>
<tr>
<td><h:outputText value= "Data de Nascimento" /></td>
<td><h:inputText id= "dtaNascimento" value= "#{clienteHandler.cliente.dataNascimento}" required= "true" >
<f:convertDateTime pattern= "ddMMyyyy" />
</h:inputText>
<h:outputText value= " * DDMMAAAA" />
<h:message for= "dtaNascimento" style= "color: darkred" /></td>
</tr>
<tr>
<td><h:outputText value= "CPF" /></td>
<td><h:inputText id= "cpf" value= "#{clienteHandler.cliente.cpf}" >
<f:validateLength minimum= "11" />
</h:inputText>
<h:outputText value= " *" />
<h:message for= "CPF" style= "color: darkred" /></td>
</tr>
<tr>
<td><h:outputText value= "RG" /></td>
<td><h:inputText id= "rg" value= "#{clienteHandler.cliente.rg}" />
<h:outputText value= " *" />
<h:message for= "RG" style= "color: darkred" /></td>
</tr>
<tr>
<td><h:outputText value= "Número Cartão SUS" /></td>
<td><h:inputText id= "cartaoSUS" value= "#{clienteHandler.cliente.cartaosus}" >
<f:validateLength minimum= "16" />
</h:inputText>
<h:outputText value= " *" />
<h:message for= "cartaoSUS" style= "color: darkred" /></td>
</tr>
<tr>
<td><h:outputText value= "Endereco" /></td>
<td><h:inputText id= "endereco" value= "#{clienteHandler.cliente.endereco}" >
<f:validateLength minimum= "10" />
</h:inputText>
<h:outputText value= " *" />
<h:message for= "endereco" style= "color: darkred" /></td>
</tr>
<tr>
<td><h:outputText value= "Bairro" /></td>
<td><h:inputText id= "bairro" value= "#{clienteHandler.cliente.bairro}" >
<f:validateLength minimum= "5" />
</h:inputText>
<h:outputText value= " *" />
<h:message for= "bairro" style= "color: darkred" /></td>
</tr>
<tr>
<td><h:outputText value= "Cidade" /></td>
<td><h:inputText id= "cidade" value= "#{clienteHandler.cliente.cidade}" >
<f:validateLength minimum= "5" />
</h:inputText>
<h:outputText value= " *" />
<h:message for= "cidade" style= "color: darkred" /></td>
</tr>
<tr>
<td> UF:</td>
<td>
<jsp:include page= "uf.jsp" />
<h:outputText value= " *" /></td>
</tr>
<tr>
<td><h:outputText value= "Mãe" /></td>
<td><h:inputText id= "mae" value= "#{clienteHandler.cliente.mae}" >
<f:validateLength minimum= "10" />
</h:inputText>
<h:outputText value= " *" />
<h:message for= "mae" style= "color: darkred" /></td>
</tr>
<tr>
<td><h:outputText value= "Pai" /></td>
<td><h:inputText id= "pai" value= "#{clienteHandler.cliente.pai}" >
<f:validateLength minimum= "10" />
</h:inputText>
<h:outputText value= " *" />
<h:message for= "pai" style= "color: darkred" /></td>
</tr>
<tr>
<td><h:outputText value= "Telefone" /></td>
<td><h:inputText id= "telefone" value= "#{clienteHandler.cliente.telefone}" />
<h:message for= "telefone" style= "color: darkred" /></td>
</tr>
<tr>
<td><h:outputText value= "Data do Cadastro" /></td>
<td><h:inputText id= "dtacadastro" readonly= "true" value= "#{clienteHandler.cliente.dataCadastro}" >
<f:convertDateTime pattern= "dd/MM/yyyy hh:mm" />
</h:inputText>
<h:message for= "dtacadastro" style= "color: darkred" /></td>
</tr>
<tr>
<td><h:outputText value= "Ativo" /></td>
<td><h:selectBooleanCheckbox id= "CliAtivo" value= "#{clienteHandler.cliente.ativo}" required= "true" />
</td>
</tr>
</table>
</rich:panel>
</h:form>
</rich:tab>
</rich:tabPanel>
<br/>
<h:panelGroup id= "MostrabtnCadCliente" >
<jsp:include page= "btnCadCliente.jsp" />
</h:panelGroup>
</f:subview>
Botoes da Classe Cliente
<% @ taglib uri = "http://richfaces.org/a4j" prefix = "a4j" %>
<% @ taglib uri = "http://richfaces.org/rich" prefix = "rich" %>
<% @ taglib uri = "http://java.sun.com/jsf/core" prefix = "f" %>
<% @ taglib uri = "http://java.sun.com/jsf/html" prefix = "h" %>
<f:subview id= "btnCadCliente" >
<h:commandButton value= "Pesquisar" action= "#{clienteHandler.pesquisaCliente}" />
<a4j:commandButton ajaxSingle= "true" value= "Limpar"
reRender= "nome" status= "commonstatus" >
</a4j:commandButton>
<h:commandButton value= "Fechar" action= "#{ddmenu.mostrarConsultarCliente}" />
</f:subview>
Handler da Classe Cliente
package br.com.saudetotal ;
import java.util.ArrayList ;
import java.util.List ;
import javax.faces.application.FacesMessage ;
import javax.faces.component.UIComponent ;
import javax.faces.component.UIParameter ;
import javax.faces.context.FacesContext ;
import javax.faces.event.ActionEvent ;
import javax.faces.validator.ValidatorException ;
import org.hibernate.Criteria ;
import org.hibernate.Session ;
import org.hibernate.criterion.Order ;
import br.com.saudetotal.dao.Dao ;
import br.com.saudetotal.modelo.Cliente ;
import br.com.saudetotal.util.HibernateUtil ;
public class ClienteHandler {
private Cliente cliente = new Cliente ();
private List < Cliente > clientes = new ArrayList < Cliente > ();
public Cliente getCliente () {
return cliente ;
}
public List < Cliente > getClientes () {
System . out . println ( "Lendo Clientes..." );
Session session = HibernateUtil . currentSession ();
Dao < Cliente > dao = new Dao < Cliente > ( session , Cliente . class );
return dao . list ();
}
public void setCliente ( Cliente cliente ) {
this . cliente = cliente ;
}
public void salva () {
System . out . println ( "Adicionando: " + cliente . getNome ());
Session session = HibernateUtil . currentSession ();
Dao < Cliente > dao = new Dao < Cliente > ( session , Cliente . class );
dao . merge ( this . cliente );
this . cliente = new Cliente ();
}
public void escolheCliente ( ActionEvent event ) {
UIComponent link = event . getComponent ();
UIParameter param = ( UIParameter ) link . findComponent ( "editId" );
Long id = ( Long ) param . getValue ();
this . cliente = this . clientes . get ( id . intValue () - 1 );
}
public List < Cliente > pesquisaCliente () {
Session session = HibernateUtil . currentSession ();
Criteria criteria = session . createCriteria ( Cliente . class );
criteria . addOrder ( Order . asc ( "nome_cli" ));
return criteria . list ();
}
public void validateCase ( FacesContext context , UIComponent component , Object value ) {
String valor = value . toString ();
if ( ! valor . matches ( "[1-0]" )) {
throw new ValidatorException ( new FacesMessage ( "Inválido" ));
}
}
public void Cancelar () {
this . cliente = new Cliente ();
}
public void validaNome ( FacesContext context , UIComponent component , Object value ) {
String nome = value . toString ();
if ( ! nome . matches ( "[A-Z.*}" )) {
throw new ValidatorException ( new FacesMessage ( "Nome Inválido" ));
}
}
}
Hibernate Util
package br.com.saudetotal.util ;
import org.apache.log4j.Logger ;
import org.hibernate.Session ;
import org.hibernate.SessionFactory ;
import org.hibernate.cfg.AnnotationConfiguration ;
public class HibernateUtil {
private static Logger logger = Logger . getLogger ( HibernateUtil . class );
private static SessionFactory sessionFactory ;
private static ThreadLocal < Session > sessions = new ThreadLocal < Session > ();
static {
sessionFactory = new AnnotationConfiguration (). configure (). buildSessionFactory ();
}
public static Session openSession () {
if ( sessions . get () != null ) {
logger . error ( "Alguém não fechou uma janela aberta !!" );
}
sessions . set ( sessionFactory . openSession ());
return sessions . get ();
}
public static void closeCurrentSession () {
sessions . get (). close ();
sessions . set ( null );
}
public static Session currentSession () {
return sessions . get ();
}
public static void loggout () {
Session s = ( Session ) sessions . get ();
s . setCacheMode ( null );
}
}
peerless 3 de ago. de 2008
[google]hibernate opensessioninview[/google]
shyricosai 4 de ago. de 2008
peerless
Agradeço pela ajuda.
Só que estou iniciando em JAVA agora, e não consegui entender muita coisa.
Não quero que me passem a correção do código nem façam outro para mim, gostaria que me explica-se o que está acontecendo e a forma de corrigir.