在JSF中使用导航
框架:JSF2.0。在JSF中使用导航,jsf,jsf-2,Jsf,Jsf 2,框架:JSF2.0。 public static void redirectUsingNavigation(String from, String outCome) { FacesContext facesContext = FacesContext.getCurrentInstance(); facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, from, outC
public static void redirectUsingNavigation(String from, String outCome) {
FacesContext facesContext = FacesContext.getCurrentInstance();
facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, from, outCome);
}
我的问题:我有一个名为login.xhtml的页面。每当有人查看该页面时,它就会调用一个过滤器,称为身份验证过滤器。过滤器将检查,如果用户已经登录,它将根据用户的角色重定向到默认值(例如:Admin将转到“Admin/Admin.xhtml”,student将重定向到“user/user.xhtml”)
我的解决方案:使用JSF导航
我的配置:
faces config.xml
我的问题:
public static void redirectUsingNavigation(String from, String outCome) {
FacesContext facesContext = FacesContext.getCurrentInstance();
facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, from, outCome);
}
该代码根本没有重定向。它只是在转发请求。它只是对不同的目标页面使用相同的请求对象)。实际重定向指示浏览器在给定的
位置
标题上发送新请求。您可以在浏览器地址栏中看到此更改
您需要向结果中添加faces redirect=true
参数来触发重定向(如果您使用隐式导航而不是冗长的导航案例,这尤其有用):
如果您希望导航案例始终发生,请将其添加到导航案例中:
*
公元
/admin/admin.xhtml
美国
/user/user.xhtml
最后但并非最不重要的一点是,您需要确保在提交响应之前调用handleNavigation()
,否则它将根本无法工作,并且服务器日志中会充斥IllegalStateException:response ready committed
错误。您可以使用
在提交响应之前调用bean操作
另见:
navigationHandler.handleNavigation(facesContext, from, outCome + "?faces-redirect=true")