JSF 2.0 自定义异常处理程序 - ViewHandler 未在 Weld 应用程序中重定向

发布于 2024-11-04 22:42:30 字数 3097 浏览 6 评论 0原文

我使用 Ed Burns 建议的自定义异常handler 每当发生特定异常时显示错误页面。我还尝试在上下文不活动时显示错误页面。 然而,这部分(第 45./46 行)会引起麻烦:

nav.handleNavigation(fc, null, "viewExpired");
fc.renderResponse();

这里出现了完整的 CustomExceptionHandler (按照 Ed Burns 在他的博客中建议的那样正确配置)

package com.sun.faces;
import java.util.Iterator;
import java.util.Map;
import javax.enterprise.context.ContextNotActiveException;
import javax.faces.FacesException;
import javax.faces.application.NavigationHandler;
import javax.faces.application.ViewExpiredException;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerWrapper;
import javax.faces.context.FacesContext;
import javax.faces.event.ExceptionQueuedEvent;
import javax.faces.event.ExceptionQueuedEventContext;

public class ViewExpiredExceptionExceptionHandler extends ExceptionHandlerWrapper {

private ExceptionHandler wrapped;

public ViewExpiredExceptionExceptionHandler(ExceptionHandler wrapped) {
    this.wrapped = wrapped;
}

@Override
public ExceptionHandler getWrapped() {
    return this.wrapped;
}

@Override
public void handle() throws FacesException {
    for (Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();) {
        ExceptionQueuedEvent event = i.next();
        ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
        Throwable t = context.getException();
        if (t instanceof ViewExpiredException) {
            ViewExpiredException vee = (ViewExpiredException) t;
            FacesContext fc = FacesContext.getCurrentInstance();
            Map<String, Object> requestMap = fc.getExternalContext().getRequestMap();
            NavigationHandler nav =
                    fc.getApplication().getNavigationHandler();
            try {
                // Push some useful stuff to the request scope for
                // use in the page
                requestMap.put("currentViewId", vee.getViewId());

                nav.handleNavigation(fc, null, "viewExpired");
                fc.renderResponse();

            } finally {
                i.remove();
            }
        }
    if (t instanceof ContextNotActiveException) {
    FacesContext fc = FacesContext.getCurrentInstance();
            NavigationHandler nav =
                    fc.getApplication().getNavigationHandler();

         try {
                nav.handleNavigation(fc, null, "contextNotActive");
                fc.renderResponse();

         } finally {
         i.remove();
     }
     }
    }
    // At this point, the queue will not contain any exceptions I want to handle (for now).
    // Therefore, let the parent handle them.
    getWrapped().handle();

    }
}

但是我总是在控制台中得到“视图无法渲染”的打印输出。那么为什么它不呈现该错误页面。我认为这是由不活动的上下文(由 NavigationHandler 使用)引起的!

任何人都可以确认此行为吗?

PS:我正在使用 Weld 1.1 和 Mojarra 2.0.4

I use the by Ed Burns suggested custom exception handler to display an error page whenever a specific exception occurs. I also try do display an error page when the context is not active.
However, this part (line 45./46.) causes trouble:

nav.handleNavigation(fc, null, "viewExpired");
fc.renderResponse();

Here comes the full CustomExceptionHandler (configured correctly as Ed Burns suggests in his Blog)

package com.sun.faces;
import java.util.Iterator;
import java.util.Map;
import javax.enterprise.context.ContextNotActiveException;
import javax.faces.FacesException;
import javax.faces.application.NavigationHandler;
import javax.faces.application.ViewExpiredException;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerWrapper;
import javax.faces.context.FacesContext;
import javax.faces.event.ExceptionQueuedEvent;
import javax.faces.event.ExceptionQueuedEventContext;

public class ViewExpiredExceptionExceptionHandler extends ExceptionHandlerWrapper {

private ExceptionHandler wrapped;

public ViewExpiredExceptionExceptionHandler(ExceptionHandler wrapped) {
    this.wrapped = wrapped;
}

@Override
public ExceptionHandler getWrapped() {
    return this.wrapped;
}

@Override
public void handle() throws FacesException {
    for (Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();) {
        ExceptionQueuedEvent event = i.next();
        ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
        Throwable t = context.getException();
        if (t instanceof ViewExpiredException) {
            ViewExpiredException vee = (ViewExpiredException) t;
            FacesContext fc = FacesContext.getCurrentInstance();
            Map<String, Object> requestMap = fc.getExternalContext().getRequestMap();
            NavigationHandler nav =
                    fc.getApplication().getNavigationHandler();
            try {
                // Push some useful stuff to the request scope for
                // use in the page
                requestMap.put("currentViewId", vee.getViewId());

                nav.handleNavigation(fc, null, "viewExpired");
                fc.renderResponse();

            } finally {
                i.remove();
            }
        }
    if (t instanceof ContextNotActiveException) {
    FacesContext fc = FacesContext.getCurrentInstance();
            NavigationHandler nav =
                    fc.getApplication().getNavigationHandler();

         try {
                nav.handleNavigation(fc, null, "contextNotActive");
                fc.renderResponse();

         } finally {
         i.remove();
     }
     }
    }
    // At this point, the queue will not contain any exceptions I want to handle (for now).
    // Therefore, let the parent handle them.
    getWrapped().handle();

    }
}

However I always get a "view could not be rendered" print-out in the console. So why is it not rendering that error page. I assume it is caused by the not active context (which is used by the NavigationHandler)!

Can anyone confirm this behavior?

P.S.: I am using Weld 1.1 with Mojarra 2.0.4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美羊羊 2024-11-11 22:42:30

我认为您必须在 faces-config.xml 文件中添加导航规则,如下所示:

 <navigation-rule>
  <from-view-id>*</from-view-id>
  <navigation-case>
   <from-outcome>contextNotActive</from-outcome>
   <to-view-id>/contextNotActive.xhtml</to-view-id>
  </navigation-case>
 </navigation-rule>

I think you've got to add a navigation rule into your faces-config.xml file, something like this:

 <navigation-rule>
  <from-view-id>*</from-view-id>
  <navigation-case>
   <from-outcome>contextNotActive</from-outcome>
   <to-view-id>/contextNotActive.xhtml</to-view-id>
  </navigation-case>
 </navigation-rule>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文