> Shouldn't the viewId be /index.jspx if that is the page I'm trying to load?
> I thought OnLoad executed during the loading of the target page? If I'm
> on page3.jspx, then I click the link to go to index.jspx shouldn't the
> onload rules for index.jspx be executed?
Yes, but you need to intercept *before* the view ID is changed. The
onload component is executed *after* the view ID has been changed.
For example:
1) user visits page1.jspx
2) onload called
3) page1.jspx rendered
4) user clicks an action button
5) action on button triggers navigation to page2.jspx
6) onload called
As you can see, the on-load is called when the new view ID is about to
be rendered, not when the code is attempting to change the view to
page2.jspx.
I have implemented this type of security in JSF using a custom view
handler. In the ViewHandler.createView method, you can check some
value (some type of authentication check) and if it fails, then change
the view. Pseudo code:
MyCustomViewHandler:
createView:
if view ID is not the login page then
check if logged in
if not, change the view ID to the login page
call super create view
This way the security is applied when the view is created, not after.
The main use case of the on-load component is more for lazy loading
data. I personally used it to load data from the database. So for
example, the user goes to a page that was bookmarked that has a list
of records. Since there is no action method, you would have to load
the data in a getter method. I didn't like that as the loading of the
data could result in an exception, and you cannot redirect after the
rendering of the page has started. So instead, I would load the
records using the on-load phase listener and if an exception occurred,
I could redirect them to a "safe" page that would not fail.
I also used it to redirect from pages that should not be bookmarked.
Say you have a wizard, and the user bookmarked page 2. Then using
on-load I could redirect the user back to page 1 from page 2 when I
see that the backing bean for page 1 was not populated.
-Andrew