【问题标题】:FacesMessages in MyFacesServlet WrapperMyFacesServlet 包装器中的 FacesMessages
【发布时间】:2011-02-24 06:11:49
【问题描述】:

我在 java bean 中抛出 NullPointerException 并在 FacesServletWrapper 中捕获异常。 在 FacesServletWrapper 中,我总是只得到 ServletException。

  1. 如何捕获我抛出的特定异常?

  2. 如何从引发异常的地方继续?

在我的 bean 中:

 public String getSize() {  
  try {
   Object object = null;
   object.equals("");

  } catch (Exception e) {
   throw new NullPointerException();
  }


 }

我的小服务程序:

 public class FacesServletWrapper extends MyFacesServlet {

 public static final String CONFIG_FILES_ATTR = "javax.faces.CONFIG_FILES";
 public static final String LIFECYCLE_ID_ATTR = "javax.faces.LIFECYCLE_ID";

 private ServletConfig servletConfig;
 private FacesContextFactory facesContextFactory;
 private Lifecycle lifecycle;

 @Override
 public void service(ServletRequest request, ServletResponse response) throws IOException, ServletException {
  FacesContext facesContext = facesContextFactory.getFacesContext(servletConfig.getServletContext(), request, response, (javax.faces.lifecycle.Lifecycle) lifecycle);
  try {
   super.service(request, response);
  } catch (Throwable e) {
   Locale locale = (Locale) facesContext.getExternalContext().getSessionMap().get(Constants.LOCALE);
   ServletContext context = servletConfig.getServletContext();
   RequestDispatcher dispatcher = context.getRequestDispatcher("/errors/error.jsf");

   if (e instanceof NullPointerException) {
                        //here I catch  only ServletException
    String error = ResourceUtil.getMessage("Login_failed", locale);
    facesContext.getExternalContext().getSessionMap().put("error", error);
    dispatcher.forward(request, response);
    ((HttpServletResponse) response).sendRedirect(((HttpServletRequest) request).getContextPath() + "/errors/error.jsf");
   } 
  }

 }

 public void destroy() {
  servletConfig = null;
  facesContextFactory = null;
  lifecycle = null;
 }

 public ServletConfig getServletConfig() {
  return servletConfig;
 }

 private String getLifecycleId() {
  String lifecycleId = servletConfig.getServletContext().getInitParameter(LIFECYCLE_ID_ATTR);
  return lifecycleId != null ? lifecycleId : LifecycleFactory.DEFAULT_LIFECYCLE;
 }

 @Override
 public void init(ServletConfig servletConfig) throws ServletException {
  super.init(servletConfig);
  this.servletConfig = servletConfig;
  facesContextFactory = (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
  LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
  lifecycle = (Lifecycle) lifecycleFactory.getLifecycle(getLifecycleId());
 }
}

谢谢!

【问题讨论】:

  • 您能否提供一个堆栈跟踪,以便我们查看它被抛出的位置?

标签: java jsf


【解决方案1】:

你在这里打电话给FacesServlet#service()

try {
    super.service(request, response);
} catch (Throwable e) {
    // ...
}

这里是its javadoc 的摘录,以了解它可能引发什么样的异常:

如果在任何一种情况下都抛出了FacesException,请从FacesException 中提取原因。如果原因是null,则从FacesException 中提取消息,将其放入新的ServletException 实例中,并将FacesException 实例作为根本原因传递,然后重新抛出ServletException 实例。如果原因是ServletException 的实例,请重新抛出原因。如果原因是IOException 的实例,请重新抛出原因。否则,创建一个新的ServletException 实例,将来自原因的消息作为第一个参数传递,将原因本身作为第二个参数传递。

换句话说,它会总是抛出ServletExceptionIOException。您需要使用Throwable#getCause() 从捕获的ServletException 中提取所需的原因,然后进一步确定。例如

if (e.getCause() instanceof NullPointerException) {
    // ...
}

【讨论】:

  • 嗨!我意识到我必须像这样经历 4 个 e.getCause():e.getCause().getCause().getCause().getCause()。我可能需要循环直到我得到一个空结果,而前一个结果将是我的例外。你对我的第二个问题有答案吗 - 我怎样才能从引发异常的地方继续?我的意思是,我想捕获异常并返回 bean 中的用户代码,就好像它是 bean 中的常规 try{}catch{} 一样?
猜你喜欢
  • 2011-02-15
  • 2011-02-24
  • 2023-03-09
  • 1970-01-01
  • 2011-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多