javax.faces.context.ExceptionHandler类的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(12.0k)|赞(0)|评价(0)|浏览(180)

本文整理了Java中javax.faces.context.ExceptionHandler类的一些代码示例,展示了ExceptionHandler类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ExceptionHandler类的具体详情如下:
包路径:javax.faces.context.ExceptionHandler
类名称:ExceptionHandler

ExceptionHandler介绍

[英]ExceptionHandler is the central point for handling unexpectedExceptions that are thrown during the Faces lifecycle. The ExceptionHandler must not be notified of any Exceptions that occur during application startup or shutdown.
See the specification prose document for the requirements for the default implementation. Exceptions may be passed to the ExceptionHandler in one of two ways:

  • by ensuring that Exceptions are not caught, or are caught and re-thrown.

This approach allows the ExceptionHandler facility specified in section JSF.6.2 to operate on the Exception.

  • By using the system event facility to publish an ExceptionQueuedEvent that wraps the Exception.

This approach requires manually publishing the ExceptionQueuedEvent, but allows more information about the Exceptionto be stored in the event. The following code is an example of how to do this.

//... 
} catch (Exception e) { 
FacesContext ctx = FacesContext.getCurrentInstance(); 
ExceptionQueuedEventContext eventContext = new ExceptionQueuedEventContext(ctx, e); 
eventContext.getAttributes().put("key", "value"); 
ctx.getApplication().publishEvent(ExceptionQueuedEvent.class, eventContext); 
}

Because the Exception must not be re-thrown when using this approach, lifecycle processing may continue as normal, allowing more Exceptions to be published if necessary.

With either approach, any ExceptionQueuedEvent instances that are published in this way are accessible to the #handlemethod, which is called at the end of each lifecycle phase, as specified in section JSF.6.2.

Note that if #handle happens to be invoked during javax.faces.event.PhaseId#RENDER_RESPONSE, the recovery options are more limited than when it is invoked during other phases. Specifically, it is not valid to call javax.faces.application.NavigationHandler#handleNavigation during RENDER_RESPONSE.

Instances of this class are request scoped and are created by virtue of FacesContextFactory#getFacesContext calling ExceptionHandlerFactory#getExceptionHandler.
[中]ExceptionHandler是处理在Faces生命周期中抛出的意外Exception的中心点。在应用程序启动或关闭期间,不得向ExceptionHandler通知任何Exceptions。
有关默认实现的要求,请参见规范文档。Exceptions可通过以下两种方式之一传递给ExceptionHandler
*通过确保Exceptions未被捕获,或被捕获并重新抛出。
这种方法允许使用JSF部分中指定的ExceptionHandler工具。6.2在Exception上操作。
*通过使用系统事件工具发布一个ExceptionQueuedEvent来包装Exception
此方法需要手动发布ExceptionQueuedEvent,但允许在事件中存储有关Exception的更多信息。下面的代码是如何执行此操作的示例。

//... 
} catch (Exception e) { 
FacesContext ctx = FacesContext.getCurrentInstance(); 
ExceptionQueuedEventContext eventContext = new ExceptionQueuedEventContext(ctx, e); 
eventContext.getAttributes().put("key", "value"); 
ctx.getApplication().publishEvent(ExceptionQueuedEvent.class, eventContext); 
}

由于使用此方法时不能重新抛出Exception,因此生命周期处理可能会继续正常进行,允许在必要时发布更多Exception
无论采用哪种方法,以这种方式发布的任何ExceptionQueuedEvent实例都可以通过#handlemethod访问,该方法在每个生命周期阶段结束时调用,如JSF部分所述。6.2.
注意,在javax期间,if#handle恰好被调用。面孔。事件PhaseId#RENDER#U响应时,恢复选项比在其他阶段调用时受到更多限制。具体来说,调用javax是无效的。面孔。应用NavigationHandler在渲染响应期间处理导航。
此类的实例在请求范围内,并通过FacesContextFactory#getFacesContext调用ExceptionHandlerFactory#getExceptionHandler创建。

代码示例

代码示例来源:origin: stackoverflow.com

try {
  // some code
} catch (Throwable e) {
  ExceptionHandler handler = handlers.get(e.getClass());
  if (handler != null) handler.handle(e);
  else DEFAULT_HANDLER.handle(e);
}
// to be continued

代码示例来源:origin: com.liferay.faces/liferay-faces-bridge-impl

protected Throwable getJSF2UnhandledException(FacesContext facesContext) {
  Throwable unhandledException = null;
  ExceptionHandler exceptionHandler = facesContext.getExceptionHandler();
  Iterable<ExceptionQueuedEvent> unhandledExceptionQueuedEvents =
    exceptionHandler.getUnhandledExceptionQueuedEvents();
  if (unhandledExceptionQueuedEvents != null) {
    Iterator<ExceptionQueuedEvent> itr = unhandledExceptionQueuedEvents.iterator();
    while (itr.hasNext()) {
      ExceptionQueuedEvent exceptionQueuedEvent = itr.next();
      ExceptionQueuedEventContext exceptionQueuedEventContext = exceptionQueuedEvent.getContext();
      unhandledException = exceptionQueuedEventContext.getException();
      break;
    }
  }
  return unhandledException;
}

代码示例来源:origin: com.liferay.faces/liferay-faces-bridge-impl

protected Throwable getJSF2HandledException(FacesContext facesContext) {
  Throwable handledException = null;
  ExceptionHandler exceptionHandler = facesContext.getExceptionHandler();
  Iterable<ExceptionQueuedEvent> handledExceptionQueuedEvents =
    exceptionHandler.getHandledExceptionQueuedEvents();
  if (handledExceptionQueuedEvents != null) {
    Iterator<ExceptionQueuedEvent> itr = handledExceptionQueuedEvents.iterator();
    while (itr.hasNext()) {
      ExceptionQueuedEvent exceptionQueuedEvent = itr.next();
      ExceptionQueuedEventContext exceptionQueuedEventContext = exceptionQueuedEvent.getContext();
      handledException = exceptionQueuedEventContext.getException();
      break;
    }
  }
  return handledException;
}

代码示例来源:origin: GluuFederation/oxAuth

public void handle() throws FacesException {
  final Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator();
  while (i.hasNext()) {
    ExceptionQueuedEvent event = i.next();
    ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
    Throwable t = context.getException();
    final FacesContext fc = FacesContext.getCurrentInstance();
    final ExternalContext externalContext = fc.getExternalContext();
    final ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler) fc.getApplication().getNavigationHandler();
    try {
      log.error(t.getMessage(), t);
      performRedirect(externalContext, "/error_service.htm");
      fc.renderResponse();
    } finally {
      i.remove();
    }
  }
  getWrapped().handle();
}

代码示例来源:origin: org.icefaces/icefaces

public void handle() throws FacesException {
  FacesContext fc = FacesContext.getCurrentInstance();
  for (Iterator<ExceptionQueuedEvent> iter = getUnhandledExceptionQueuedEvents().iterator(); iter.hasNext(); ) {
    ExceptionQueuedEvent event = iter.next();
    ExceptionQueuedEventContext queueContext = (ExceptionQueuedEventContext) event.getSource();
    Throwable ex = queueContext.getException();
    //walk back to the original cause of the exception
    while (ex.getCause() != null) {
      ex = ex.getCause();
    }
    String redirectURL = mapping.get(ex.getClass());
    if (redirectURL != null) {
      PartialResponseWriter writer = fc.getPartialViewContext().getPartialResponseWriter();
      String uri = fc.getApplication().getViewHandler().getResourceURL(fc, redirectURL);
      try {
        writer.startDocument();
        writer.redirect(uri);
        writer.endDocument();
        fc.responseComplete();
        return;
      } catch (IOException e) {
        throw new FacesException(e);
      }
    }
  }
  handler.handle();
}

代码示例来源:origin: org.apache.deltaspike.modules/deltaspike-jsf-module-impl

(ExceptionQueuedEventContext) exceptionQueuedEventIterator.next().getSource();
  Throwable throwable = exceptionQueuedEventContext.getException();
  FacesContext facesContext = exceptionQueuedEventContext.getContext();
  Flash flash = facesContext.getExternalContext().getFlash();
    if (facesContext.getViewRoot() != null)
      viewId = facesContext.getViewRoot().getViewId();
this.wrapped.handle();

代码示例来源:origin: org.apache.myfaces.tobago/tobago-core

@Override
public void handle() throws FacesException {
 final FacesContext facesContext = FacesContext.getCurrentInstance();
   && facesContext.getPartialViewContext().isAjaxRequest()
   && getUnhandledExceptionQueuedEvents().iterator().hasNext()) {
  final Throwable exception = getUnhandledExceptionQueuedEvents().iterator().next().getContext().getException();
     && (facesContext.getCurrentPhaseId() != PhaseId.RENDER_RESPONSE
     || !facesContext.getExternalContext().isResponseCommitted())) {
 getWrapped().handle();

代码示例来源:origin: mysticfall/pivot4j

ExceptionQueuedEvent event = it.next();
  ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event
      .getSource();
  FacesContext facesContext = FacesContext.getCurrentInstance();
  ResourceBundle bundle = facesContext.getApplication()
      .getResourceBundle(facesContext, "msg");
  Throwable t = context.getException();
  Throwable cause = ExceptionUtils.getRootCause(t);
  facesContext.addMessage(null, new FacesMessage(
      FacesMessage.SEVERITY_ERROR, title, message));
getWrapped().handle();

代码示例来源:origin: org.glassfish/javax.faces

ExceptionQueuedEventContext eventContext = new ExceptionQueuedEventContext(context, t);
  context.getApplication().publishEvent(context, ExceptionQueuedEvent.class, eventContext);
  context.getExceptionHandler().handle();
} finally {
  ApplicationAssociate.setCurrentInstance(null);

代码示例来源:origin: br.gov.frameworkdemoiselle/demoiselle-jsf

@Override
public void handle() throws FacesException {
  FacesContext facesContext = FacesContext.getCurrentInstance();
  ExceptionQueuedEventContext exceptionContext;
  Throwable root;
  for (Iterator<ExceptionQueuedEvent> iter = getUnhandledExceptionQueuedEvents().iterator(); iter.hasNext();) {
    exceptionContext = (ExceptionQueuedEventContext) iter.next().getSource();
    root = getRoot(exceptionContext.getException());
    if (handleException(root, facesContext)) {
      iter.remove();
    }
  }
  if (getUnhandledExceptionQueuedEvents().iterator().hasNext()) {
    getWrapped().handle();
  }
}

代码示例来源:origin: com.github.adminfaces/admin-template

@Override
public void handle() throws FacesException {
  FacesContext context = FacesContext.getCurrentInstance();
  findErrorMessages(context);
  handleException(context);
  wrapped.handle();
}

代码示例来源:origin: org.apache.openwebbeans/openwebbeans-jsf

@Override
  public void handle() throws FacesException
  {
    Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator();
    while (i.hasNext())
    {
      ExceptionQueuedEvent event = i.next();
      ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();

      // get the exception from context
      Throwable t = context.getException();
      if (NonexistentConversationException.class.isInstance(t))
      {
        i.remove();
        throw RuntimeException.class.cast(t);
      }
    }
    delegate.handle();
  }
}

代码示例来源:origin: omnifaces/omnifaces

/**
 * Set every exception as a global FATAL faces message.
 */
@Override
public void handle() {
  for (Iterator<ExceptionQueuedEvent> iter = getUnhandledExceptionQueuedEvents().iterator(); iter.hasNext();) {
    addGlobal(new FacesMessage(SEVERITY_FATAL, createFatalMessage(iter.next().getContext().getException()), null));
    iter.remove();
  }
  getWrapped().handle();
}

代码示例来源:origin: org.apache.myfaces.tobago/tobago-jsf-compat

public static void handleExceptions(FacesContext facesContext) {
 facesContext.getExceptionHandler().handle();
}

代码示例来源:origin: org.apache.deltaspike.modules/deltaspike-jsf-module-impl-ee6

private boolean isUnhandledExceptionQueued(FacesContext context)
{
  return context.getExceptionHandler().getUnhandledExceptionQueuedEvents() != null &&
      context.getExceptionHandler().getUnhandledExceptionQueuedEvents().iterator().hasNext();
}

代码示例来源:origin: org.apache.myfaces.core/myfaces-api

@Override
public Iterable<ExceptionQueuedEvent> getUnhandledExceptionQueuedEvents()
{
  return getWrapped().getUnhandledExceptionQueuedEvents();
}

代码示例来源:origin: javax/javaee-web-api

/**
 * <p>The default behavior of this method is to call
 * {@link ExceptionHandler#getHandledExceptionQueuedEvents()} on the wrapped
 * {@link ExceptionHandler} object.</p>
 *
 * @see ExceptionHandler#getHandledExceptionQueuedEvents()
 */
@Override
public Iterable<ExceptionQueuedEvent> getHandledExceptionQueuedEvents() {
  return getWrapped().getHandledExceptionQueuedEvents();
}

代码示例来源:origin: com.sun.faces/jsf-api

/**
 * <p>The default behavior of this method is to
 * call {@link javax.faces.context.ExceptionHandler#processEvent(javax.faces.event.SystemEvent)}
 * on the wrapped {@link ExceptionHandler} object.</p>
 *
 * @see javax.faces.context.ExceptionHandler#processEvent(javax.faces.event.SystemEvent)
 */
@Override
public void processEvent(SystemEvent event) throws AbortProcessingException {
  getWrapped().processEvent(event);
}

代码示例来源:origin: com.sun.faces/jsf-api

/**
 * <p>The default behavior of this method is to
 * call {@link javax.faces.context.ExceptionHandler#getRootCause(Throwable)}
 * on the wrapped {@link ExceptionHandler} object.</p>
 *
 * @see javax.faces.context.ExceptionHandler#getRootCause(Throwable)
 */
@Override
public Throwable getRootCause(Throwable t) {
  return getWrapped().getRootCause(t);
}

代码示例来源:origin: javax/javaee-web-api

/**
 * <p>The default behavior of this method is to
 * call {@link ExceptionHandler#getHandledExceptionQueuedEvent()}
 * on the wrapped {@link ExceptionHandler} object.</p>
 *
 * @see ExceptionHandler#getHandledExceptionQueuedEvent()
 */
@Override
public ExceptionQueuedEvent getHandledExceptionQueuedEvent() {
  return getWrapped().getHandledExceptionQueuedEvent();
}

相关文章