开发者

FacesMessages in MyFacesServlet Wrapper

开发者 https://www.devze.com 2022-12-31 22:34 出处:网络
I throw NullPointerException in a java bean and catch the exception in FacesServletWrapper. in FacesServletWrapper I gets always only ServletException.

I throw NullPointerException in a java bean and catch the exception in FacesServletWrapper. in FacesServletWrapper I gets always only ServletException.

  1. how can I catch the specific exception that I throw?

  2. How can I continue from where I throws the exception?

in my bean:

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

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


 }

my 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().getInitPara开发者_JAVA百科meter(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());
 }
}

Thanks!


You're calling FacesServlet#service() here:

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

Here's an extract from its javadoc to learn what kind of exception it may throw:

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.

In other words, it will always throw either ServletException or IOException. You need to use Throwable#getCause() to extract the desired cause from the catched ServletException and then determine it further. E.g.

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

精彩评论

暂无评论...
验证码 换一张
取 消