Pagina de boas vidas com jsf e hibernate

3 respostas
Y

Senhores, sou iniciante e estou com uma divida recente
Gostaria de colocar na minha pagina de boas vidas o nome do usuario o que for precisar ....

Aqui esta meus codigos:

package br.com.serjaum.mb;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.com.serjaum.facade.PessoaFacade;
import br.com.serjaum.facade.PessoaFacadeImpl;
import br.com.serjaum.modelo.Pessoa;

public class PessoaMB implements Serializable {
	
	private static final long serialVersionUID = -333995781063775201L;

	private Pessoa pessoa = new Pessoa();

	private Long id;

	public PessoaMB(){
		System.out.println(" >>>>>>>>>>>>>>>>>>>> Contrutor do PESSOA_MB <<<<<<<<<<<<<<<<<<");
		
		if(this.pessoa == null){
			this.pessoa = new Pessoa(); 
		}
	}
	
	public String login() throws Exception{
        boolean logado = false;
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        HttpServletResponse rp = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        HttpServletRequest rq = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        PessoaFacade pessoaService = new PessoaFacadeImpl();
		
        logado = pessoaService.autentica(this.pessoa.getEmail(), this.pessoa.getSenha());
           
        if(logado){
            session.setAttribute("user", logado);
            rp.sendRedirect(rq.getContextPath() + "/pages/index.jsf");
            return "success";
        }else{
            session.setAttribute("user", null);
            session.removeAttribute("user");
            rp.sendRedirect(rq.getContextPath() + "/pages/login/login.jsf");
            return "failure";
        }
    }
	
	public String logout() {
		HttpServletRequest rq = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		HttpServletResponse rp = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
		FacesContext facesContext = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
		session.invalidate();
		try {
			rp.sendRedirect(rq.getContextPath() + "/pages/login/login.jsf");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return "logoutOk";
	}
	
	public String save(){
		PessoaFacade pessoaService = new PessoaFacadeImpl();
				
		pessoaService.salva(this.pessoa);
		
		this.pessoa = new Pessoa(); 
		return "cadastraSucesso";
	}

	public String delete(){
		PessoaFacade pessoaService = new PessoaFacadeImpl();
		this.pessoa.setId(id);
		pessoaService.remove(this.pessoa);
		this.pessoa = new Pessoa(); 
		
		return "removeSucesso";
	}
	
	public String merge(){
		PessoaFacade pessoaService = new PessoaFacadeImpl();
		pessoaService.atualiza(this.pessoa);
		this.pessoa = new Pessoa(); 

		return "atualizaSucesso";
	}
	
	public String load(){
		PessoaFacade pessoaService = new PessoaFacadeImpl();
		this.pessoa = pessoaService.procura(this.id);
		
		return "pesquisaSucesso";
	}

	public String pesquisaByNome(){
		PessoaFacadeImpl pessoaService = new PessoaFacadeImpl();
		this.pessoa = pessoaService.procuraByNome(this.pessoa.getNome());
		
		return "pesquisaByNomeSucesso";
	}
	
	public List<Pessoa> getPessoas(){
		PessoaFacade pessoaService = new PessoaFacadeImpl();		
		
		return pessoaService.lista();
	}

	public List<Pessoa> getPessoasByNome(){ 
		PessoaFacade pessoaService = new PessoaFacadeImpl();

		List<Pessoa> lista = pessoaService.pesquisaPessoasByNome(this.pessoa.getNome());
	
		return lista;
	}
	
	public Pessoa getPessoa() {
		return pessoa;
	}

	public void setPessoa(Pessoa pessoa) {
		this.pessoa = pessoa;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
}
Essa é minha página:
<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="StyleSheet" type="text/css" href="/jsf/style/estilos.css" media="screen" />
<script type="text/javascript" src="/jsf/resources/jquery.maskedinput-1.2.1.js"></script>
</head>

<body>
<f:view>
	<h:form>
		<h:messages />
		<h2><h:outputText value="Ola, #{PessoaMB.getEmail}"/></h2>
		<fieldset>
			<legend>Cadastro de Pessoas Físicas</legend> 
				<h:panelGrid>
					<h:commandLink value="Adicionar pessoa" action="toCadastraPessoa" />
					<h:commandLink value="Pesquisar pessoa" action="toPesquisaPessoa" />
					<h:commandLink value="Remover pessoa" action="toRemovePessoa" />
					<h:commandLink value="Atualizar pessoa" action="toAtualizaPessoa" />
					<h:commandLink value="Sair" action="#{pessoaMB.logout}" />
				</h:panelGrid>
		</fieldset>
	</h:form>
</f:view>
</body>
</html>

Preciso de ajuda !
Aguardo instruções!

3 Respostas

Alexandre_Saudate
YANI:
Senhores, sou iniciante e estou com uma divida recente Gostaria de colocar na minha pagina de boas vidas o nome do usuario o que for precisar ....

Aqui esta meus codigos:

package br.com.serjaum.mb;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.com.serjaum.facade.PessoaFacade;
import br.com.serjaum.facade.PessoaFacadeImpl;
import br.com.serjaum.modelo.Pessoa;

public class PessoaMB implements Serializable {
	
	private static final long serialVersionUID = -333995781063775201L;

	private Pessoa pessoa = new Pessoa();

	private Long id;

	public PessoaMB(){
		System.out.println(" >>>>>>>>>>>>>>>>>>>> Contrutor do PESSOA_MB <<<<<<<<<<<<<<<<<<");
		
		if(this.pessoa == null){
			this.pessoa = new Pessoa(); 
		}
	}
	
	public String login() throws Exception{
        boolean logado = false;
        PessoaFacade pessoaService = new PessoaFacadeImpl();
		
        logado = pessoaService.autentica(this.pessoa.getEmail(), this.pessoa.getSenha());
           
        if(logado){
            this.pessoa = logado;

            return "success";
        }else{            
            return "failure";
        }
    }
	
	public String logout() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
		session.invalidate();

		return "logoutOK";
	}
	
	public String save(){
		PessoaFacade pessoaService = new PessoaFacadeImpl();
				
		pessoaService.salva(this.pessoa);		
		
		return "cadastraSucesso";
	}

	public String delete(){
		PessoaFacade pessoaService = new PessoaFacadeImpl();
		this.pessoa.setId(id);
		pessoaService.remove(this.pessoa);
		
		return "removeSucesso";
	}
	
	public String merge(){
		PessoaFacade pessoaService = new PessoaFacadeImpl();
		pessoaService.atualiza(this.pessoa);
		this.pessoa = new Pessoa(); 

		return "atualizaSucesso";
	}
	
	public String load(){
		PessoaFacade pessoaService = new PessoaFacadeImpl();
		this.pessoa = pessoaService.procura(this.id);
		
		return "pesquisaSucesso";
	}

	public String pesquisaByNome(){
		PessoaFacadeImpl pessoaService = new PessoaFacadeImpl();
		this.pessoa = pessoaService.procuraByNome(this.pessoa.getNome());
		
		return "pesquisaByNomeSucesso";
	}
	
	public List<Pessoa> getPessoas(){
		PessoaFacade pessoaService = new PessoaFacadeImpl();		
		
		return pessoaService.lista();
	}

	public List<Pessoa> getPessoasByNome(){ 
		PessoaFacade pessoaService = new PessoaFacadeImpl();

		List<Pessoa> lista = pessoaService.pesquisaPessoasByNome(this.pessoa.getNome());
	
		return lista;
	}
	
	public Pessoa getPessoa() {
		return pessoa;
	}

	public void setPessoa(Pessoa pessoa) {
		this.pessoa = pessoa;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
}
Essa é minha página:
<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="StyleSheet" type="text/css" href="/jsf/style/estilos.css" media="screen" />
<!--Você não vai precisar de javascript puro com JSF (isso inclui jQuery) -->
</head>

<body>
<f:view>
	<h:form>
		<h:messages />
		<h2><h:outputText value="Ola, #{PessoaMB.pessoa.email}"/></h2>
		<fieldset>
			<legend>Cadastro de Pessoas Físicas</legend> 
				<h:panelGrid>
					<h:commandLink value="Adicionar pessoa" action="toCadastraPessoa" />
					<h:commandLink value="Pesquisar pessoa" action="toPesquisaPessoa" />
					<h:commandLink value="Remover pessoa" action="toRemovePessoa" />
					<h:commandLink value="Atualizar pessoa" action="toAtualizaPessoa" />
					<h:commandLink value="Sair" action="#{pessoaMB.logout}" />
				</h:panelGrid>
		</fieldset>
	</h:form>
</f:view>
</body>
</html>

Preciso de ajuda !
Aguardo instruções!

Ajuste o código do MB pra maneira como deixei acima. Ajuste o faces-config.xml com as saídas certas ("success" e "failure") e também ajuste o escopo dele para session (tudo no faces-config.xml). Lembre- se de que um MB dificilmente precisa ter acesso direto à Session, então, não faz sentido ajustar atributos lá "na mão". Além disso, o ideal é você ter uma instância gerenciada (no seu caso, pessoa, no MB) e fazer as operações que você precisar com ela. Os acessos a atributos são feitos colocando diretamente o nome do atributo ("#{PessoaMB.pessoa.email}").

[]´s

Y

Certo mas eu fiz desse jeito também:

<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="StyleSheet" type="text/css" href="/jsf/style/estilos.css" media="screen" />
<script type="text/javascript" src="/jsf/resources/jquery.maskedinput-1.2.1.js"></script>
</head>

<body>
<f:view>
	<h:form>
		<h:messages />
		<h2><h:outputText value="Ola, #{pessoaMB.pessoa.nome}"/></h2>
		<fieldset>
			<legend>Cadastro de Pessoas Físicas</legend> 
				<h:panelGrid>
					<h:commandLink value="Adicionar pessoa" action="toCadastraPessoa" />
					<h:commandLink value="Pesquisar pessoa" action="toPesquisaPessoa" />
					<h:commandLink value="Remover pessoa" action="toRemovePessoa" />
					<h:commandLink value="Atualizar pessoa" action="toAtualizaPessoa" />
					<h:commandLink value="Sair" action="#{pessoaMB.logout}" />
				</h:panelGrid>
		</fieldset>
	</h:form>
</f:view>
</body>
</html>

e o faces-config com session...

Alguém pode me ajudar ... preciso de ajuda sou iniciante no negocio ...

Y

mando de novo meu codigo, se estava completo...

package br.com.serjaum.mb;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.Session;

import br.com.serjaum.dao.PessoaDAO;
import br.com.serjaum.facade.PessoaFacade;
import br.com.serjaum.facade.PessoaFacadeImpl;
import br.com.serjaum.modelo.Pessoa;

public class PessoaMB implements Serializable {
	
	private static final long serialVersionUID = -333995781063775201L;

	private Pessoa pessoa = new Pessoa();
	private String nomeUser;
	private PessoaDAO pessoaDAO;
	private	Session session;

	/**
	 * @return the nomeUser
	 */
	public String getNomeUser() {
		return nomeUser;
	}

	/**
	 * @param nomeUser the nomeUser to set
	 */
	public void setNomeUser(String nomeUser) {
		this.nomeUser = nomeUser;
	}

	private Long id;

	public PessoaMB(){
		System.out.println(" >>>>>>>>>>>>>>>>>>>> Contrutor do PESSOA_MB <<<<<<<<<<<<<<<<<<");
		
		if(this.pessoa == null){
			this.pessoa = new Pessoa(); 
		}
	}
	
	public String login() throws Exception{
        boolean logado = false;
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        HttpServletResponse rp = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        HttpServletRequest rq = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        PessoaFacade pessoaService = new PessoaFacadeImpl();
		Pessoa pessoa = new Pessoa();
        logado = pessoaService.autentica(this.pessoa.getEmail(), this.pessoa.getSenha());
           
        if(logado){
            session.setAttribute("user", logado);
           // pessoaDAO = new PessoaDAO((Session) session, Pessoa.class);
           // nomeUser = PessoaMB.class.getName().toString();
            rp.sendRedirect(rq.getContextPath() + "/pages/index.jsf");
            return "success";
        }else{
            session.setAttribute("user", null);
            session.removeAttribute("user");
            rp.sendRedirect(rq.getContextPath() + "/pages/login/login.jsf");
            return "failure";
        }
    }
	
	public String logout() {
		HttpServletRequest rq = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		HttpServletResponse rp = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
		FacesContext facesContext = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
		session.invalidate();
		try {
			rp.sendRedirect(rq.getContextPath() + "/pages/login/login.jsf");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return "logoutOk";
	}
	
	public String save(){
		PessoaFacade pessoaService = new PessoaFacadeImpl();
				
		pessoaService.salva(this.pessoa);
		
		this.pessoa = new Pessoa(); 
		return "cadastraSucesso";
	}

	public String delete(){
		PessoaFacade pessoaService = new PessoaFacadeImpl();
		this.pessoa.setId(id);
		pessoaService.remove(this.pessoa);
		this.pessoa = new Pessoa(); 
		
		return "removeSucesso";
	}
	
	public String merge(){
		PessoaFacade pessoaService = new PessoaFacadeImpl();
		pessoaService.atualiza(this.pessoa);
		this.pessoa = new Pessoa(); 

		return "atualizaSucesso";
	}
	
	public String load(){
		PessoaFacade pessoaService = new PessoaFacadeImpl();
		this.pessoa = pessoaService.procura(this.id);
		
		return "pesquisaSucesso";
	}

	public String pesquisaByNome(){
		PessoaFacadeImpl pessoaService = new PessoaFacadeImpl();
		this.pessoa = pessoaService.procuraByNome(this.pessoa.getNome());
		
		return "pesquisaByNomeSucesso";
	}
	
	public List<Pessoa> getPessoas(){
		PessoaFacade pessoaService = new PessoaFacadeImpl();		
		
		return pessoaService.lista();
	}

	public List<Pessoa> getPessoasByNome(){ 
		PessoaFacade pessoaService = new PessoaFacadeImpl();

		List<Pessoa> lista = pessoaService.pesquisaPessoasByNome(this.pessoa.getNome());
	
		return lista;
	}
	
	public Pessoa getPessoa() {
		return pessoa;
	}

	public void setPessoa(Pessoa pessoa) {
		this.pessoa = pessoa;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
}
Criado 10 de março de 2010
Ultima resposta 10 de mar. de 2010
Respostas 3
Participantes 2