views:

455

answers:

1

I am wondering the following:

When a request comes into the JSF servlet, is it possible to redirect to a session timeout page if a session bean indicates that the session has not been logged in?

For example:

<navigation-rule>
    <from-view-id>*</from-view-id>
    <navigation-case>
        <if>#{not userBean.loggedIn}</if>
        <to-view-id>/login.jsf</to-view-id>
        <redirect/>
    </navigation-case>
</navigation-rule>

Obviously it would need to exclude the login.jsf from this rule...but you get the point. I have tried something similar and it does not appear to work, and even if it did, how would it deal with Bookmarked URLS?

How best to implement this in JSF2? Also, is there anything like the global.asax from ASP.NET in JSF2, or are PhaseListeners/Filters about it?

Thank you.

+1  A: 

Navigation rules would not be a good place for this sort of functionality since the NavigationHandler is not invoked until quite late in the lifecycle (Invoke Application).

alt text


You can use declarative security (in the web.xml) to redirect to a login page. Alternatively, you can redirecting using a Filter if you want finer grain control over what happens.

McDowell
I am currently looking into using PartialViewContextFactory decoration, and PartialViewContext, PartialViewContextWriter to implement what I am looking for...I want to be able to have my Ajax responses force the main window to redirect to the session timeout page if the Ajax request was performed on a session that has timed out.You're answer is very clear and really helpful. Thank you.
CDSO1
@CDSO1 - I'm not sure how the context will help you. Generally, the lifecycle should throw a `ViewExpiredException` when data is submitted to an expired session at the Restore View phase (though I haven't checked the AJAX behaviour). This would be handled by the ViewHandler. I imagine that any redirect for AJAX will require sending some form of message to the client and redirecting via JavaScript.
McDowell
Basically, I am looking to include some script on ALL ajax responses from the server. Something like "<script>resetSessionTimeout()</script>.I have been looking for a solution, but have still not resolved this.
CDSO1
How would I tap into a ResponseWriter in a PhaseListener for an ajax request? Currently, using the writer, I get invalid XML error, where my context is either written before the ajax response (beforePhase) or after the ajax response (afterPhase)...is there another way to integrate into the process?
CDSO1