根据当前视图处理 ViewExpiredException

2024-01-07

我在我的项目中使用 JSF 2.0 和 Primefaces。

我有两个 xhtml 页面,即 Cars.xhtml 和 Bikes.xhtml。

我正在使用 ViewScoped 支持 bean。

目前,如果从两个页面中的任何一个获取视图过期异常,我都会在 web.xml 中处理它。 通过错误页面标签并定向到welcome.xhtml。

现在如果我从 Bikes.xhtml 收到 viewexpired 异常 我需要定向到另一个页面,即 BikesHome.xhtml 而不是welcome.xhtml。

如果异常来自 Cars.xhtml,则应显示welcome.xhtml。

请帮我看看该怎么做。


我对此不是 100% 确定(因为我自己还没有尝试过),但这是我的建议 - 看一下这个优雅地处理 JSF2 中的 ViewExpiredException http://weblogs.java.net/blog/edburns/archive/2009/09/03/dealing-gracefully-viewexpiredexception-jsf2.

if (t instanceof ViewExpiredException) {
    ViewExpiredException vee = (ViewExpiredException) t;

此时你可以得到view id如下 -

vee.getViewId();

然后基于view id进行所需的导航。

NavigationHandler nh = facesContext.getApplication().getNavigationHandler();
//check condition, set view       
nv.handleNavigation(facesContext, null, "/view-id?faces-redirect=true");
facesContext.renderResponse();

或者,我想你也可以这样做——

FacesContext.getExternalContext().redirect(url);
FacesContext.responseComplete();

重定向。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

根据当前视图处理 ViewExpiredException 的相关文章

随机推荐