Question

Im trying to prevent users to access special pages with a phaselistener. for that reason im trying to figure out on which page they try to access.

but my problem is, i only get the page they where before. not the actual page.

public void afterPhase(PhaseEvent event) 
{
    FacesContext fc = event.getFacesContext();
    System.out.println("test1" + fc.getViewRoot().getViewId());
}

and same here

public void afterPhase(PhaseEvent event) 
{
    FacesContext fc = event.getFacesContext();
    HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest();
    String uri = request.getRequestURI();

    System.out.println("uri: " + uri);
}

why is that, and how do i get the pagename the user is trying to access? Not that one that they required one step before, or better the page they are coming from.

Was it helpful?

Solution

It is one step behind because that is the way sequence of HTTP POST request behaves. When you are navigating in JSF application via command buttons each request goes as a post request.

Since you are protecting some resources make sure they are accessed via HTTP GET than you will get exact view id, this can be achieved as

  • User directly hits the url from address bar of browser.
  • After a post of jsf app redirect it to the resource instead of simple JSF navigation. POST-REDIRECT-GET pattern falls into this have a look here.
  • If you are showing some messages after each POST, you might need Flash map for that, which is new feature in JSF2, if you are on JSF1.x hard luck, you can implement flash if you want to.

To conclude catch the view ids on HTTP GET request.

Hope this helps...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top