JSF problem in Logoff method

Asked

Viewed 271 times

1

Good night

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

This is the Managebean:

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

And here’s the XHTML commandLink:

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

The login screen and the screen that this logoff button are in separate folders

the login is on pages/public/login.xhtml

the button is on pages/templates/header.xhtml

Thanks for your help.

1 answer

1


There are some points that should be considered:

  1. actionListener, use in cases where you need to run a view-related logic, where there is no need to exchange page.
  2. The page that is returning in the method logoff does not indicate that it is a redirect, by default the page forward.

For components Primefaces, use action instead of actionListener, when there is need to exchange pages.

As for your method of logoff, to indicate to JSF that there is redirection, add faces-redirect=true on 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";     
}

Calling for:

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

Browser other questions tagged

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