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

📄 baseexceptionhandler.java

📁 struts+spring+hibernate自创框架
💻 JAVA
字号:
package com.pegasus.framework.exception;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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;

import com.pegasus.framework.constant.ErrorConstant;

public class BaseExceptionHandler extends ExceptionHandler {
	private Log logger = LogFactory.getLog(BaseExceptionHandler.class);

	/**
	 * @param exception
	 *            Exception
	 * @param config
	 *            ExceptionConfig
	 * @param mapping
	 *            ActionMapping
	 * @param form
	 *            ActionForm
	 * @param request
	 *            HttpServletRequest
	 * @param response
	 *            HttpServletResponse
	 * @return ActionForward
	 * @throws ServletException
	 */
	public ActionForward execute(Exception exception, ExceptionConfig config, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
		logger.info("BaseExceptionHandler execute");
		ActionMessages errors = new ActionMessages();
		ActionForward forward = null;
		ActionMessage error = null;
		String property = null;
		String path = null;
		if (config.getPath() != null) {
			path = config.getPath();
		} else {
			path = mapping.getInput(); // input表示struts配置文件的input值
		}

		// 获取异常配置的Key值
		error = new ActionMessage(ErrorConstant.ERROR_MESSAGE, exception.getMessage());
		property = error.getKey();
		this.storeException(request, property, error, forward, config.getScope(), errors);
		return forward;
	}

	// 用于设置在request还是在session中保存异常信息,一般保存在request当中
	protected void storeException(HttpServletRequest request, String property, ActionMessage error, ActionForward forward, String scope, ActionMessages errors) {
		errors.add(property, error);
		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 + -