Error redirecting to a jsf page inside a bean

0

(RESOLVED) Error redirecting to a jsf page within a ManagerBean. I have an application in Java EE, and I want to validate within the LoginBean constructor (ViewScope) to redirect to my main page if there is already a session open and valid for that browser.

I can retrieve the session, check if it is successful, but can not redirect before opening the login screen.

The following error is returned:

Cannot call sendRedirect() after the response has been committed

Bean code

@ManagedBean
@ViewScoped
public class LoginBean implements Serializable {

    private static final long serialVersionUID = 3654371476703737165L;

    public String testarSessaoLogada() throws IOException {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        HttpSession session = request.getSession(true);

        LoginControle loginControle = (LoginControle) session.getAttribute("loginControle");

        if (loginControle.getControleSessaoBean() != null && session != null) {
            SessaoLogada sessaoLogada = loginControle.getControleSessaoBean().buscarSessaoLogada(session.getId());
            if (sessaoLogada != null) {
                if (sessaoLogada.getUsuario().getTipo().equals("Cliente")) {
                    FuncoesUtils.redirect("");
                } else {
                    try {
                        FacesContext.getCurrentInstance().getExternalContext().redirect("privado/controle.jsf");
                    } catch (Exception e) {
                        try {
                            FacesContext.getCurrentInstance().getExternalContext().redirect("privado/controle.jsf");
                        } catch (IOException ex) {
                            Logger.getLogger(LoginBean.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            }
        }
        return "";
    }
}

On the login.html page you have the following code that has the function load function that tests if a login session already exists for that browser ...

The error occurs just when you try to redirect

FacesContext.getCurrentInstance().getExternalContext().redirect("privado/controle.jsf");

I needed to figure out the error or even a tip to do this redirect in another way .... Thank you in advance.

Resolution:

link

Only instead of loading the function into a screen component I loaded it into the before h: body.

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
       ...
    </h:head>
    <f:event type="preRenderView" listener="#{loginBean.testarSessaoLogada()}" />
    <h:body>
        ...

Thanks to all who helped me ...

    
asked by anonymous 31.03.2017 / 22:51

1 answer

1

If the call is being made through a actionListner or a action , in the method itself you can return a string with the private / control.jsf value, if necessary a redirect get to update the private URL / control.jsf? faces-redirect = true

public String testarSessaoLogada() throws IOException {
    HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    HttpSession session = request.getSession(true);

    LoginControle loginControle = (LoginControle) session.getAttribute("loginControle");

    if (loginControle.getControleSessaoBean() != null && session != null) {
        SessaoLogada sessaoLogada = loginControle.getControleSessaoBean().buscarSessaoLogada(session.getId());
        return isClienteLogado(sessaoLogada) ? "" : "privado/controle.jsf";
    }

    return "";
}

private boolean isClienteLogado(SessaoLogada sessao){
    return sessao != null && sessao.getUsuario().getTipo().equals("Cliente");
}
    
03.04.2017 / 04:49