Framework: JSF 2.0.
My problem: I have a page, called login.xhtml. Whenever someone view that page, it invoked a filter, called Authentication filter. The filter will check, if user already loged in, it will be redirected to default based on user’s role (for example: Admin will go to “admin/admin.xhtml”, student will be redirected to “user/user.xhtml”).
My solution: Using the JSF Navigation
My config:
faces-config.xml
<navigation-rule>
<from-view-id>*</from-view-id>
<navigation-case>
<from-outcome>AD</from-outcome>
<to-view-id>/admin/admin.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>US</from-outcome>
<to-view-id>/user/user.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
The redirection using navigation:
public static void redirectUsingNavigation(String from, String outCome) {
FacesContext facesContext = FacesContext.getCurrentInstance();
facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, from, outCome);
}
My question:
- When i run the redirectUsingNavigation(“*”,”AD”) or redirectUsingNavigation(null,”AD”), it does not redirect me to admin.xhtml (i’m still on login.xhtml). How to fix this?
- Any framework support me on this problem?
That code is not redirecting at all. It’s just forwarding the request. It’s just using the same request object for a different target page). A real redirect instructs the browser to send a new request on the given
Locationheader. You see this change being reflected back in browser address bar.You need to either add
faces-redirect=trueparameter to the outcome to trigger the redirect (this is particularly useful if you’re using implicit navigation instead of verbose navigation cases):Or add
<redirect/>to the navigation cases if you want them to always take place:Last but not least, you need to ensure that
handleNavigation()is called before the response is committed, otherwise it won’t work at all and your server logs would be littered withIllegalStateException: response already committederrors. You can make use of<f:event type="preRenderView">to invoke a bean action before the response is committed.See also: