tags:

views:

303

answers:

2

Hi, is there a way to redirect all NullPointerExceptions to a pretty jsp page perhaps in Struts?

Thanks in advance!

+1  A: 

There's a request scoped EXCEPTION object that will contain the exception. See:

http://struts.apache.org/1.x/struts-core/apidocs/constant-values.html

and the following key:

org.apache.struts.action.EXCEPTION

There are then various ways to print that out in the JSP, e.g using a Struts bean tag:

<logic:equal name="org.apache.struts.action.EXCEPTION" value="java.lang.NullPointerException" scope="request">
  <bean:write property="org.apache.struts.action.EXCEPTION"/>
</logic:equal>

This is all Struts 1.x however. I'm sure Struts 2 has a similar way.

Jon
+4  A: 

There is a struts-config.xml configuration that allows you to define a exception hanbler:

  <global-exceptions>
    <exception handler="br.com.nostrum.radiomanager.exception.RadioManagerExceptionHandler" key="exception" type="java.lang.Exception" />
  </global-exceptions>

Here we catched java.lang.Exception and his descendants but you can changed to NullPointerException. An the handler should extend org.apache.struts.action.ExceptionHandler and forward via mapping.findForward (like @Kevin Crowell said) or other method of yous choice.

Hope that helps.

Leonel Martins