Class ExceptionHandler

java.lang.Object
org.apache.struts.action.ExceptionHandler

public class ExceptionHandler extends Object

An ExceptionHandler is configured in the Struts configuration file to handle a specific type of exception thrown by an Action.execute method.

Since:
Struts 1.1
  • Field Details

    • INCLUDE_PATH

      public static final String INCLUDE_PATH

      The name of a configuration property which can be set to specify an alternative path which should be used when the HttpServletResponse has already been committed.

      To use this, in your struts-config.xml specify the exception handler like this:

         <exception
             key="GlobalExceptionHandler.default"
             type="java.lang.Exception"
             path="/ErrorPage.jsp">
             <set-property key="INCLUDE_PATH" value="/error.jsp" />
         </exception>
        

      You would want to use this when your normal ExceptionHandler path is a Tiles definition or otherwise unsuitable for use in an include context. If you do not use this, and you do not specify "SILENT_IF_COMMITTED" then the ExceptionHandler will attempt to forward to the same path which would be used in normal circumstances, specified using the "path" attribute in the <exception> element.

      Since:
      Struts 1.3
      See Also:
    • SILENT_IF_COMMITTED

      public static final String SILENT_IF_COMMITTED

      The name of a configuration property which indicates that Struts should do nothing if the response has already been committed. This suppresses the default behavior, which is to use an "include" rather than a "forward" in this case in hopes of providing some meaningful information to the browser.

      To use this, in your struts-config.xml specify the exception handler like this:

         <exception
             key="GlobalExceptionHandler.default"
             type="java.lang.Exception"
             path="/ErrorPage.jsp">
             <set-property key="SILENT_IF_COMMITTED" value="true" />
         </exception>
        
      To be effective, this value must be defined to the literal String "true". If it is not defined or defined to any other value, the default behavior will be used.

      You only need to use this if you do not want error information displayed in the browser when Struts intercepts an exception after the response has been committed.

      Since:
      Struts 1.3
      See Also:
  • Constructor Details

    • ExceptionHandler

      public ExceptionHandler()
  • Method Details

    • execute

      public ActionForward execute(Exception ex, ExceptionConfig ae, ActionMapping mapping, ActionForm formInstance, HttpServletRequest request, HttpServletResponse response) throws ServletException

      Handle the Exception. Return the ActionForward instance (if any) returned by the called ExceptionHandler.

      Parameters:
      ex - The exception to handle
      ae - The ExceptionConfig corresponding to the exception
      mapping - The ActionMapping we are processing
      formInstance - The ActionForm we are processing
      request - The servlet request we are processing
      response - The servlet response we are creating
      Returns:
      The ActionForward instance (if any) returned by the called ExceptionHandler.
      Throws:
      ServletException - if a servlet exception occurs
      Since:
      Struts 1.1
    • handleCommittedResponse

      protected void handleCommittedResponse(Exception ex, ExceptionConfig config, ActionMapping mapping, ActionForm formInstance, HttpServletRequest request, HttpServletResponse response, ActionForward actionForward)

      Attempt to give good information when the response has already been committed when the exception was thrown. This happens often when Tiles is used. Base implementation will see if the INCLUDE_PATH property has been set, or if not, it will attempt to use the same path to which control would have been forwarded.

      Parameters:
      ex - The exception to handle
      config - The ExceptionConfig we are processing
      mapping - The ActionMapping we are processing
      formInstance - The ActionForm we are processing
      request - The servlet request we are processing
      response - The servlet response we are creating
      actionForward - The ActionForward we are processing
      Since:
      Struts 1.3
    • determineIncludePath

      protected String determineIncludePath(ExceptionConfig config, ActionForward actionForward)

      Return a path to which an include should be attempted in the case when the response was committed before the ExceptionHandler was invoked.

      If the ExceptionConfig has the property INCLUDE_PATH defined, then the value of that property will be returned. Otherwise, the ActionForward path is returned.

      Parameters:
      config - Configuration element
      actionForward - Forward to use on error
      Returns:
      Path of resource to include
      Since:
      Struts 1.3
    • logException

      protected void logException(Exception e)

      Logs the Exception using commons-logging.

      Parameters:
      e - The Exception to log.
      Since:
      Struts 1.2
    • storeException

      protected void storeException(HttpServletRequest request, String property, ActionMessage error, ActionForward forward, String scope)

      Default implementation for handling an ActionMessage generated from an Exception during Action delegation. The default implementation is to set an attribute of the request or session, as defined by the scope provided (the scope from the exception mapping), if error is not null. Otherwise, an ActionMessages instance is created, the error is added to the collection and the collection is set under the Globals.ERROR_KEY.

      Parameters:
      request - The request we are handling
      property - The property name to use for this error
      error - The error generated from the exception mapping
      forward - The forward generated from the input path (from the form or exception mapping)
      scope - The scope of the exception mapping.
      Since:
      Struts 1.2