Keep Bean alive even after a redirect - JSF

0

Does anyone know of any way to make a Bean live even after giving a redirect?

The situation looks like this: I'm developing a project where, in theory, the user logs in and the user and password he typed are saved somewhere (probably together with the bean) until he clicks LogOut.

I looked for a solution on the internet and found the JSF Flash but in flash it does not save the data forever, it loses the data after 2 or more redirects, I also tested the CDI @ConversationScoped but the bean dies when I I give redirect so I do not know how to keep this bean alive, can anyone help me?

    
asked by anonymous 19.08.2018 / 23:11

1 answer

0

You can put in the session attributes and / or objects that will be kept until it is destroyed. My code below refers to a method of a class that gets the client email set by another method, all within the same session that can be fetched in any Bean.

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

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

public void inserirClienteEmail(Cliente cliente, boolean clienteLogado) {
    FacesContext fc = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
    session.setAttribute("LOGADO", clienteLogado);
    session.setAttribute("CLIENTEID", cliente.getEmail());
}

I use this way whenever I need the data just use these methods = D I hope I have helped

    
24.08.2018 / 04:05