⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 baseexceptionhandler.java

📁 一个基于Java的新闻发布系统
💻 JAVA
字号:
/*******************************************************************************
 * 文件名: BaseExceptionHandler.java <br>
 * 版本: <br>
 * 描述: 自定义异常Handler类,继承自ExceptionHandler<br>
 * 版权所有: <br>
 * //////////////////////////////////////////////////////// <br>
 * 创建者: 杨赞明 <br>
 * 创建日期: 2005-8-2 <br>
 * 修改者: <br>
 * 修改日期: <br>
 * 修改说明: <br>
 ******************************************************************************/
package com.hope.common.exception;

import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ExceptionHandler;
import org.apache.struts.config.ExceptionConfig;

/**
 * 自定义异常Handler类,继承自ExceptionHandler,配合BaseException类对异常进行处理
 */
public class BaseExceptionHandler extends ExceptionHandler {

    /*
     * (非 Javadoc)
     *
     * @see org.apache.struts.action.ExceptionHandler#execute(java.lang.Exception,
     *      org.apache.struts.config.ExceptionConfig,
     *      org.apache.struts.action.ActionMapping,
     *      org.apache.struts.action.ActionForm,
     *      javax.servlet.http.HttpServletRequest,
     *      javax.servlet.http.HttpServletResponse)
     */
    /**
     * 继承自ExceptionHandler的方法
     *
     * @param ex Exception
     * @param config ExceptionConfig
     * @param mapping ActionMapping
     * @param formInstance ActionForm
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @return ActionForward
     * @throws ServletException
     */
    public ActionForward execute(Exception ex, ExceptionConfig config,
            ActionMapping mapping, ActionForm formInstance,
            HttpServletRequest request, HttpServletResponse response)
            throws ServletException {
        //定义ActionMessages,ActionMessage等对象
        ActionMessages errors = new ActionMessages();
        ActionForward forward = null;
        ActionMessage error = null;
        String property = null;

        //通过参数config获取config的路径
        String path = null;
        if (config.getPath() != null) {
            path = config.getPath();
        }
        //如果路径不存在,则取input页面的路径
        else {
            path = mapping.getInput();
        }
        forward = new ActionForward(path);

        //判断参数ex的异常是否是BaseException类型
        if (ex instanceof BaseException) {
            /*
             * // BaseException baseException = (BaseException) ex; String
             * messageKey = baseException.getMessageKey(); Object[] exArgs =
             * baseException.getMessageArgs(); if(exArgs != && exArgs.length>0) {
             * error = new ActionMessage(messageKey, exArgs); }else{
             *  }
             */

            //调用processBaseException方法对异常信息进行处理
            processBaseException((BaseException) ex, config, request, forward,
                    property, errors);

            //将异常封装到List中
            List exceptions = ((BaseException) ex).getExceptions();
            //判断异常是否存在,存在则对异常List进行遍历,分别对异常进行处理
            if (exceptions != null && !exceptions.isEmpty()) {
                int size = exceptions.size();
                Iterator iter = exceptions.iterator();
                while (iter.hasNext()) {
                    BaseException subException = (BaseException) iter.next();
                    processBaseException(subException, config, request,
                            forward, property, errors);
                }
            }
        }
        //如果不是BaseException类的异常,则获取异常的key值,并保存异常信息
        else {
            error = new ActionMessage(config.getKey());
            property = error.getKey();
            storeException(request, property, error, forward,
                    config.getScope(), errors);
        }
        return forward;
    }

    /**
     * 接收异常信息,针对信息的存在情况创建不同的ActionMessage对象
     *
     * @param ex BaseException
     * @param config ExceptionConfig
     * @param request HttpServletRequest
     * @param forward ActionForward
     * @param property String
     * @param errors ActionMessages
     */
    protected void processBaseException(BaseException ex,
            ExceptionConfig config, HttpServletRequest request,
            ActionForward forward, String property, ActionMessages errors) {
        //接收异常信息,判断信息是否存在,根据信息存在的情况对ActionMessage对象的创建
        //采用不同的方式
        String messageKey = ex.getMessageKey();
        ActionMessage error = null;
        Object[] exArgs = ex.getMessageArgs();
        if (exArgs != null && exArgs.length > 0) {
            error = new ActionMessage(messageKey, exArgs);
        }
        else {
            error = new ActionMessage(messageKey);
        }
        //将方法中参数传入storeException方法中
        storeException(request, property, error, forward, config.getScope(),
                errors);
    }

    /*
     * (非 Javadoc)
     *
     * @see org.apache.struts.action.ExceptionHandler#storeException(javax.servlet.http.HttpServletRequest,
     *      java.lang.String, org.apache.struts.action.ActionMessage,
     *      org.apache.struts.action.ActionForward, java.lang.String)
     */
    /**
     * 定义保存异常信息的方法,判断scope的类型,根据scope的不同进行不同的异常信息设置
     *
     * @param request HttpServletRequest
     * @param property String
     * @param error ActionMessage
     * @param forward ActionForward
     * @param scope String
     * @param errors ActionMessages
     */
    protected void storeException(HttpServletRequest request, String property,
            ActionMessage error, ActionForward forward, String scope,
            ActionMessages errors) {
        //将error和property添加到errors中
        errors.add(property, error);
        //根据scope的不同,采用不同的保存异常信息的方式
        if ("request".equals(scope)) {
            request.setAttribute(Globals.ERROR_KEY, errors);
        }
        else {
            request.getSession().setAttribute(Globals.ERROR_KEY, errors);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -