java - MyFacesServlet 包装器中的 FacesMessages

标签 java jsf

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

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

  2. 如何从抛出异常的地方继续?

在我的 bean 中:

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

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


 }

我的servlet:

 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());
 }
}

谢谢!

最佳答案

您在此处调用 FacesServlet#service():

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

以下是 its javadoc 的摘录了解它可能抛出什么样的异常:

If a FacesException is thrown in either case, extract the cause from the FacesException. If the cause is null extract the message from the FacesException, put it inside of a new ServletException instance, and pass the FacesException instance as the root cause, then rethrow the ServletException instance. If the cause is an instance of ServletException, rethrow the cause. If the cause is an instance of IOException, rethrow the cause. Otherwise, create a new ServletException instance, passing the message from the cause, as the first argument, and the cause itself as the second argument.

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

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

关于java - MyFacesServlet 包装器中的 FacesMessages,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2914412/

相关文章:

jsf - 如何显示以逗号或分号分隔的值列表

javascript - 在离开当前页面的浏览器上触发数据库调用?

java - Eclipse 项目名称更改

java - zmq jar 的版本

java - 如何使用buffer读写java对象

java - 当远程计算机中未安装 cygwin 时,使用 Java 连接到远程 Windows 计算机

java - 更改 ace 中浏览的默认按钮名称 :FileEntry

java - 使用 JAR 内的 list 编译 JAR?

jsf - 记录器,从@Inject 转换为生产者

jsf - JSF/PrimeFaces 中的单页主从