Doubts the transitions of an object between Beans JSF

0

I have a login class that instantiates a CLIENTID attribute in the session after the client logs in

LoginBean.java

public void efetuarLogin() throws IOException{
    cliente = clientePU.clientePorEmail(email);
    System.out.println(email);
    System.out.println(senha1);
    try{
        if(cliente.getEmail().matches(email) && cliente.getSenha().matches(senha1)){
            clienteLogado = true;
            FacesContext fc = FacesContext.getCurrentInstance();
            HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
            session.setAttribute("LOGADO", clienteLogado);
            session.setAttribute("CLIENTEID", cliente.getEmail());
            FacesContext.getCurrentInstance().getExternalContext().redirect("/sitecarpal/carrinho.xhtml");
        }
        else message.error("usuario ou senha invalido");
    }catch (NullPointerException e) {
        // TODO: handle exception
        message.error("Usuario nao existe");
    }
}

I have a class that will be responsible for returning the client with all its attributes to other Beans through its Get Client method that uses CDI to inject the dependencies returned from the clientPU class by JPA which got the client by the ID (email) that It was taken in my session.

SessionOja.java

@SessionScoped
@Named
public class SessaoObjeto implements Serializable{
    private static final long serialVersionUID = 1L;

    @Inject
    private ClientePU clientePU;
    private Cliente cliente;

    public SessaoObjeto(){
        cliente = new Cliente();
    }

    public Cliente obterCliente(){
        HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
                .getRequest();
        HttpServletRequest request = (HttpServletRequest) req;
        HttpSession session = (HttpSession) request.getSession();
        String email = (String) session.getAttribute("CLIENTEID");
        cliente = clientePU.clientePorEmail(email);
        return cliente;
    }

And finally I have the Bean class that will take all my client data to my view, but the Client Object is not returned by the GetClient method of the SessionObject class, does not return null or anything ...

EditCadastroBean.java

@SessionScoped
@Named
public class EditarCadastroBean implements Serializable {
    private static final long serialVersionUID = 1L;

    private Cliente cliente;

    public EditarCadastroBean() {
        cliente = new Cliente();
    }

    public void iniciarEdicao() {
        try {
            cliente = new SessaoObjeto().obterCliente();
        } catch (NullPointerException e) {
            // TODO: handle exception
        }
    }

The fact is that when I use the GetClient method of the SessionObject class returning a String with the ID (email) I get in the session it returns the string quietly ... The doubt is, because the String transits between the classes and the Client Object not? This forces me to implement @Inject in the Bean class and then use it to get the client, since this could already have been done by the GetClient method ... getting like this:

CadastroBean.Java

public void iniciarEdicao() {
    try {
        cliente = clientePU.clientePorEmail(new SessaoObjeto().obterCliente());
    } catch (NullPointerException e) {
        // TODO: handle exception
    }
}

SessionObject.Java

public String obterCliente(){
    HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
            .getRequest();
    HttpServletRequest request = (HttpServletRequest) req;
    HttpSession session = (HttpSession) request.getSession();
    String email = (String) session.getAttribute("CLIENTEID");
    return email;
}
    
asked by anonymous 16.08.2018 / 20:43

0 answers