Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Stop Rendering This Page, Render a Different Page Instead?

 
Wally Hartshorn
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This seems like it's probably quite simple, but I'm stumped.

Here's the scenario:
  • User authenticates via Tomcat's form login.
  • Tomcat looks in web.xml and sees "index.jsp" listed as the welcome file.
  • Tomcat displays index.jsp, which contains a single line, redirecting to "mainpage.faces".
  • JSF steps in and starts processing mainPage.faces.
  • Java code determines that this the first time this user has logged in, so it wants to send them to firstUse.faces instead.

  • At that point I'm stuck. How do I get JSF to abandon trying to display mainPage.faces and instead switch to firstUse.faces?

    I suppose that one way would be to have the index.jsp file send them to a "click here to begin" page first, rather than mainPage.faces. The command button that the user would click would have action="#{mainPageBean.display}", which would return either "mainPage" or "firstUse" as the outcome. This would allow JSF to display the proper page, but it's very inelegant and user-hostile to require the user to click a button to start. Ugh.

    A better way would be to have the redirect in index.jsp send them to "begin.faces", which in turn would look something like this:



    In my imagination, that would cause a redirect either to mainPage.faces or to firstUse.faces, depending on the outcome returned by mainPageBean.display(). Still somewhat inelegant, but better than "click here to begin".

    Of course, when I tried that, I got a 404 error, since (I think) the jsp:forward was trying to redirect to "mainPage" or to "firstUse" rather than to "mainPage.faces" or to "firstUse.faces". I supposed I could just change it to something like this:



    Even if that might work, it feels icky.

    So, what's the correct way to do what I'm trying to accomplish? How do I get JSF to abandon rendering of the current page and switch to rendering of a different page?
     
    Maurizio Nagni
    Ranch Hand
    Posts: 75
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Let me repeate if i understod what you want after the user login
    a) is the first time -----> go to firstUse.faces
    b) is not the first time -----> go to mainpage.faces

    I suppose that for your "login" is one action from a button which executes some method in some backbean.
    The most simple solution for you is to make a kind of


    the faces servlet will use the returned String to read from the faces-config.xml the page to render next.
    ehm... obviously if you want to store informations like first time of loggin you need a long term storage, that is a database or at least a text file, but it is a totally different argument.....
     
    Wally Hartshorn
    Ranch Hand
    Posts: 77
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Maurizio Maurizio:
    I suppose that for your "login" is one action from a button which executes some method in some backbean.
    The most simple solution for you is to make a kind of



    Thanks, but unfortunately that won't work in this case. We're using Tomcat's authentication stuff, which means that there is no login method. If the user isn't logged in, Tomcat steps in and handles that for us. Our code only starts executing after the user has logged in. It's at that point, while it is preparing to display the main page, that our code can determine that this is the user's first login and that therefore a different page needs to be displayed (where we ask them some setup questions).
     
    Wally Hartshorn
    Ranch Hand
    Posts: 77
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I found a bit of code in this article
    that made me think that I could use something like this:



    When I tried that, the current page continued to attempt to render (and failing, of course, because it's trying to get information from the user's non-existent profile). That led me to think that the above code might send me to the right page, but that I need something else to get JSF to abandon rendering the current page.

    On further reading of the article, though, I began to wonder if the problem is impossible to solve at the point where I'm trying to solve it. In the article, this snippet of code is in a class that extends NavigationHandler and is named in the faces-config.xml file in a navigation-handler element.

    Am I wandering completely down the wrong path on this? Is subclassing the NavigationHandler class the only way to accomplish this? (Anytime something that I think should be simple starts getting this complicated, I suspect that I'm missing something really easy.)
     
    Ravindra Rawat
    Ranch Hand
    Posts: 34
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can try the following:



    This should be accompanied by a navigation case in faces-config for outcome "firstUsage" from your view id.

    Warm Regards
    Ravindra
     
    Wally Hartshorn
    Ranch Hand
    Posts: 77
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Ravindra Rawat:
    You can try the following:




    Well, shoot, that looks very promising, but it doesn't seem to work. I thought perhaps the order of the statements was simply reversed, so I swapped them, but it still wouldn't work. My webapp simply refuses to stop trying to call getters for properties in mainPage.jsp.

    In Sun's Life Cycle of a JavaServer Faces Page, the section about the "Apply Request Values Phase" states:

    At this point, if the application needs to redirect to a different web application resource or generate a response that does not contain any JavaServer Faces components, it can call FacesContext.responseComplete.


    That sounds like exactly what I'm trying to do -- redirect to another outcome -- but JSF seems to be ignoring me. I'm stumped.

    Any other ideas?
     
    Wally Hartshorn
    Ranch Hand
    Posts: 77
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I just want to emphasize that I'm trying to do this stuff within a normal backing bean. Will FacesContext.responseComplete work within such a situation, or does it need to be in some sort of listener or navigation handler?
     
    And then we all jump out and yell "surprise! we got you this tiny ad!"
    the new thread boost feature brings a LOT of attention to your favorite threads
    https://coderanch.com/t/674455/Thread-Boost-feature
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!