0

I have a JSF 2.0 app that has a bean called agreement. This app has 4 pages in total: main.xhtml, details.xhtml, generation.xhtml and generationFailed.xhtml. The right order of navigation should be main.xhtml -> details.xhtml and given certain conditions, either generation.xhtml or generationFailed.xhtml.

The order has to be like that because in order to correctly display details.xhtml and the generation pages, a bean has to be built based on the user's input in main.xhtml.

The problem is that the customer can go directly to any of the pages by entering the URL address in the browser. So, when loading for example details.xhtml, it will throw an error because the agreement bean has several null attributes. I have to somehow know if the agreement bean's attribute is null (the attribute is called id), and if it is null, force a redirect to main.xhtml.

Any ideas?

Nacho321
  • 1,911
  • 7
  • 33
  • 55
  • You can do what you want using session validation like [this](http://stackoverflow.com/questions/8144195/check-if-session-exists-jsf) – danRod Oct 24 '13 at 17:30

4 Answers4

1

You have to use filters for pages, and build a virtual state machine for each of your clients. The state machine keeps the navigation rule that you described: main.xhtml -> details.xhtml -> either generation.xhtml or generationFailed.xhtml. By saving the state in cookies or whatever you like (in session or by server side somehow) you can manage by checking the actual state, if the actual request is legit or not..

LMG
  • 966
  • 1
  • 12
  • 28
1

Simply when the user navigate from the main.xhtml to any page put the main.xhtml bean's attribute in flash scope by

       public String gotoDetails() {  
       getFlash().put("id", Id);  
       return "/pages/details?faces-redirect=true";  
  }

then in the Details bean check for this attribute to ensure that it is not null otherwise redirect the user to the main page to select first an order or whatever is.use this method for checking

      public void loadPage(ComponentSystemEvent cse) {  
       if (null == getID()) {  
            FacesContext  
                      .getCurrentInstance()  
                      .getApplication()  
                      .getNavigationHandler()  
                      .handleNavigation(FacesContext.getCurrentInstance(), null,  
                                "/pages/main?faces-redirect=true");  
       }  
  }  

then you can call the previous method in preRenderView event in the details.xhtml by using

    <f:event listener="#{yourdetailsbean.loadPage}" type="preRenderView" />   
hanan Ahmed
  • 442
  • 3
  • 5
0

Well, if the user enters a URL the browser will send a GET request, but if they submit a form it will submit a POST request. You could simply write a servlet Filter that redirects all GET requests for the FacesServlet to the welcome page.

meriton
  • 68,356
  • 14
  • 108
  • 175
0

Create a sessionscoped bean and add 3 booleans:

firstPageVisited, secondPageVisited, thirdPageVisited.

On page 4 check if thirdPageVisited is false, redirect to 3, on 3 if secondPageVisited is false redirect to 2...

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319