JSF problem in the Logoff method

0

Good evening

I have the following problem, I have a JSF application and I need to make a logoff command, I did several and all gave the same result, it "kills" the user session (logs), but does not redirect to login screen (which is screen I want it to come back) good here are the codes:

This is ManageBean:

    public String logoff() {
    FacesContext fc = FacesContext.getCurrentInstance();  
    HttpSession session = (HttpSession)fc.getExternalContext().getSession(false);  
    session.invalidate();   
    return "/pages/public/index.xhtml";     
}

And here is the XHTML commandLink:

<p:commandLink id="logoff" value="Teste"
                actionListener="#{autenticacaoManageBean.logoff}"  ajax="false" />

The login screen and the screen that the logoff button is in separate folders

login is in pages/public/login.xhtml

the button is in pages/templates/header.xhtml

Thanks in advance for the help.

    
asked by anonymous 08.06.2017 / 05:45

1 answer

1

There are a few things to keep in mind:

  • actionListener , use in cases where you need to execute a logic related to the view, where there is no need to change pages.
  • The page that is returning in method logoff does not indicate that it is a redirect, by default it will be page forward .
  • For Primefaces components, use action instead of actionListener , when there is a need for page exchange. Home As for your logoff method, to tell JSF that there is redirect, add faces-redirect=true to the return of your page.

    Bean:

    public String logoff() {
        FacesContext fc = FacesContext.getCurrentInstance();  
        HttpSession session = (HttpSession)fc.getExternalContext().getSession(false);  
        session.invalidate();   
        return "/pages/public/index.xhtml?faces-redirect=true";     
    }
    

    Call:

    <p:commandLink id="logoff" value="Teste"
                    action="#{autenticacaoManageBean.logoff}" ajax="false" />
    
        
    08.06.2017 / 13:04