4

我在我的项目中使用 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。

请帮我怎么做。

4

1 回答 1

9

I not 100% sure about this (because I haven't tried it myself) but here is my suggestion for - Check this out Dealing Gracefully with ViewExpiredException in JSF2.

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

At this point you can get the view id as follows -

vee.getViewId();

And then based on the view id do the desired navigation.

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

Or, I think you could also do -

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

to redirect.

于 2011-09-27T18:19:01.277 回答