SpecialExceptionHandler.jbak 2.57 KB
Newer Older
O'Reilly Media, Inc. committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
package com.oreilly.struts.chapter10examples;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ExceptionHandler;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionError;
import org.apache.struts.util.AppException;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.config.ExceptionConfig;
import com.oreilly.struts.framework.exceptions.BaseException;
public class SpecialExceptionHandler extends ExceptionHandler {
    protected ActionForward execute( Exception ex, ExceptionConfig config, ActionMapping mapping, ActionForm formInstance, HttpServletRequest request, HttpServletResponse response ) throws ServletException {
        ActionForward forward = null;
        ActionError error = null;
        String property = null;
        
        /* Get the path for the forward either from the exception element
        * or from the input attribute.
        */
        String path = null;
        if( config.getPath() != null ) {
            path = config.getPath();
        }
        else {
            path = mapping.getInput();
        }
        
        // Construct the forward object
        forward = new ActionForward( path );
        
        /* Figure out what type of exception has been thrown. The Struts
        * AppException is not being used in this example.
        */
        if( ex instanceof BaseException ) {
            
            // This is the specialized behavior
            BaseException baseException = (BaseException)ex;
            String messageKey = baseException.getMessageKey();
            Object[] exArgs = baseException.getMessageArgs();
            if( exArgs != null && exArgs.length > 0 ) {
                
                // If there were args provided, use them in the ActionError
                error = new ActionError( messageKey, exArgs );
            }
            else {
                
                // Create an ActionError without any arguments
                error = new ActionError( messageKey );
            }
        }
        else {
            error = new ActionError( config.getKey() );
            property = error.getKey();
        }
        
        // Store the ActionError into the proper scope
        
        // The storeException method is defined in the parent class
        storeException( request, property, error, forward, config.getScope() );
        return forward;
    }
}