Class ExceptionHandler
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 Summary
Modifier and TypeFieldDescriptionstatic final String
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.static final String
The name of a configuration property which indicates that Struts should do nothing if the response has already been committed. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected 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 theExceptionHandler
was invoked.execute
(Exception ex, ExceptionConfig ae, ActionMapping mapping, ActionForm formInstance, HttpServletRequest request, HttpServletResponse response) Handle the Exception.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.protected void
Logs theException
using commons-logging.protected void
storeException
(HttpServletRequest request, String property, ActionMessage error, ActionForward forward, String scope) Default implementation for handling anActionMessage
generated from anException
duringAction
delegation.
-
Field Details
-
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
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 handleae
- The ExceptionConfig corresponding to the exceptionmapping
- The ActionMapping we are processingformInstance
- The ActionForm we are processingrequest
- The servlet request we are processingresponse
- The servlet response we are creating- Returns:
- The
ActionForward
instance (if any) returned by the calledExceptionHandler
. - 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 handleconfig
- The ExceptionConfig we are processingmapping
- The ActionMapping we are processingformInstance
- The ActionForm we are processingrequest
- The servlet request we are processingresponse
- The servlet response we are creatingactionForward
- The ActionForward we are processing- Since:
- Struts 1.3
-
determineIncludePath
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 propertyINCLUDE_PATH
defined, then the value of that property will be returned. Otherwise, the ActionForward path is returned.- Parameters:
config
- Configuration elementactionForward
- Forward to use on error- Returns:
- Path of resource to include
- Since:
- Struts 1.3
-
logException
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 anException
duringAction
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), iferror
is notnull
. Otherwise, anActionMessages
instance is created, the error is added to the collection and the collection is set under theGlobals.ERROR_KEY
.- Parameters:
request
- The request we are handlingproperty
- The property name to use for this errorerror
- The error generated from the exception mappingforward
- The forward generated from the input path (from the form or exception mapping)scope
- The scope of the exception mapping.- Since:
- Struts 1.2
-