1

I have many managed beans in @SessionScoped and I want to clear the whole session when a user logs out. Does anyone know how to achieve this? And which is the best performing: @SessionScoped or @RequestScoped or @ViewScoped? Also is there annotation to replace the redirects in file faces-config.xml?

Can I replace the following by annotation:

  <navigation-rule>
    <from-view-id>pages/login.xhtml</from-view-id>
    <navigation-case>
      <from-outcome>userOK</from-outcome>
      <to-view-id>pages/template.xhtml</to-view-id>
      <redirect />
    </navigation-case>
    <navigation-case>
      <from-outcome>userNOK</from-outcome>
      <to-view-id>pages/login.xhtml</to-view-id>
    </navigation-case>    
  </navigation-rule>
ThisDarkTao
  • 1,062
  • 10
  • 27
Hayi
  • 6,972
  • 26
  • 80
  • 139

1 Answers1

4

In your managed bean

 public void logout() {
     FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
 }

See also this answer

SECOND PART

No, there is no annotation for this. But you can always add to your link "/home.xhtml?faces-redirect=true".

And for the performance - it always depends what you want. If it's just a page which is load only once (like some report or something), then use a RequestScoped bean(it lives only per one request).

ViewScoped lives as long as the user stays on the same view, so it's good for let's say page with dynamic table which is making ajax calls.

And SessionScoped bean lives entire session, so it's good to hold a shopping cart or something like that.

Community
  • 1
  • 1
Petr Mensik
  • 26,874
  • 17
  • 90
  • 115