Keep Bean alive even after a redirect - JSF

Asked

Viewed 100 times

0

Does anyone know any way to leave a Bean alive even after giving a redirect?

The situation is like this: I am developing a project where, in theory, the user login and the user and password he typed are saved somewhere (probably along with the bean) until he clicks Logout.

I searched for a solution on the internet and I found the JSF Flash but in the flash it does not store the data forever, it loses the data after 2 or more Redirects, I also tried @Conversationscoped from CDI but the bean dies when I give redirect so I don’t know how to keep that bean alive, someone can help me?

1 answer

0


You can place attributes and/or objects in the session that will be kept until it is destroyed. My code below refers to a method of a class that gets email from the client set by another method, all within the same session that can be picked up 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 forever I need the data just use these methods =D I hope I’ve helped

Browser other questions tagged

You are not signed in. Login or sign up in order to post.