Home » xwork-2.1.5 » com.opensymphony » xwork2 » interceptor » [javadoc | source]
public class: ExceptionMappingInterceptor [javadoc | source]

All Implemented Interfaces:

This interceptor forms the core functionality of the exception handling feature. Exception handling allows you to map an exception to a result code, just as if the action returned a result code instead of throwing an unexpected exception. When an exception is encountered, it is wrapped with an ExceptionHolder and pushed on the stack, providing easy access to the exception from within your result. Note: While you can configure exception mapping in your configuration file at any point, the configuration will not have any effect if this interceptor is not in the interceptor stack for your actions. It is recommended that you make this interceptor the first interceptor on the stack, ensuring that it has full access to catch any exception, even those caused by other interceptors.

Interceptor parameters:

The parameters above enables us to log all thrown exceptions with stacktace in our own logfile, and present a friendly webpage (with no stacktrace) to the end user.

Extending the interceptor:

If you want to add custom handling for publishing the Exception, you may override #publishException(com.opensymphony.xwork2.ActionInvocation, ExceptionHolder) . The default implementation pushes the given ExceptionHolder on value stack. A custom implementation could add additional logging etc.

Example code:

    <package name="default" extends="xwork-default">
            <result name="error" type="freemarker">error.ftl</result>

            <exception-mapping exception="java.lang.Exception" result="error"/>

        <action name="test">
            <interceptor-ref name="exception"/>
            <interceptor-ref name="basicStack"/>
            <exception-mapping exception="com.acme.CustomException" result="custom_error"/>
            <result name="custom_error">custom_error.ftl</result>
            <result name="success" type="freemarker">test.ftl</result>

This second example will also log the exceptions using our own category com.mycompany.app.unhandled at WARN level.

  <package name="something" extends="xwork-default">
         <interceptor-stack name="exceptionmappingStack">
             <interceptor-ref name="exception">
                 <param name="logEnabled">true</param>
                 <param name="logCategory">com.mycompany.app.unhandled</param>
                 <param name="logLevel">WARN</param>	        		
             <interceptor-ref name="i18n"/>
             <interceptor-ref name="staticParams"/>
             <interceptor-ref name="params"/>
             <interceptor-ref name="validation">
                 <param name="excludeMethods">input,back,cancel,browse</param>

     <default-interceptor-ref name="exceptionmappingStack"/>
          <result name="unhandledException">/unhandled-exception.jsp</result>

          <exception-mapping exception="java.lang.Exception" result="unhandledException"/>
     <action name="exceptionDemo" class="org.apache.struts2.showcase.exceptionmapping.ExceptionMappingAction">
         <exception-mapping exception="org.apache.struts2.showcase.exceptionmapping.ExceptionMappingException"
         <result name="input">index.jsp</result>
         <result name="success">success.jsp</result>            
         <result name="damm">damm.jsp</result>


Field Summary
protected static final  Logger LOG     
protected  Logger categoryLogger     
protected  boolean logEnabled     
protected  String logCategory     
protected  String logLevel     
Method from com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor Summary:
doLog,   findResultFromExceptions,   getDepth,   getLogCategory,   getLogLevel,   handleLogging,   intercept,   isLogEnabled,   publishException,   setLogCategory,   setLogEnabled,   setLogLevel
Methods from com.opensymphony.xwork2.interceptor.AbstractInterceptor:
destroy,   init,   intercept
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor Detail:
 protected  void doLog(Logger logger,
    Exception e) 
    Performs the actual logging.
 protected String findResultFromExceptions(List<ExceptionMappingConfig> exceptionMappings,
    Throwable t) 
 public int getDepth(String exceptionMapping,
    Throwable t) 
    Return the depth to the superclass matching. 0 means ex matches exactly. Returns -1 if there's no match. Otherwise, returns depth. Lowest depth wins.
 public String getLogCategory() 
 public String getLogLevel() 
 protected  void handleLogging(Exception e) 
    Handles the logging of the exception.
 public String intercept(ActionInvocation invocation) throws Exception 
 public boolean isLogEnabled() 
 protected  void publishException(ActionInvocation invocation,
    ExceptionHolder exceptionHolder) 
    Default implementation to handle ExceptionHolder publishing. Pushes given ExceptionHolder on the stack. Subclasses may override this to customize publishing.
 public  void setLogCategory(String logCatgory) 
 public  void setLogEnabled(boolean logEnabled) 
 public  void setLogLevel(String logLevel)