📄 systemexceptionhandler.java
字号:
package com.bjsxt.oa.web;
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.log4j.Logger;
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.ExceptionHandler;
import org.apache.struts.config.ExceptionConfig;
import com.bjsxt.oa.managers.SystemException;
/**
* 通用异常处理器
* @author Administrator
*
*/
public class SystemExceptionHandler extends ExceptionHandler {
//直接使用Log4j的API
//private static Logger logger = Logger.getLogger(SystemExceptionHandler.class);
//使用commons-log来记录日志
private static Log logger = LogFactory.getLog(SystemExceptionHandler.class);
@Override
public ActionForward execute(Exception ex, ExceptionConfig ae, ActionMapping mapping, ActionForm formInstance, HttpServletRequest request, HttpServletResponse response) throws ServletException {
ActionForward forward = null;
if (ae.getPath() != null) {
forward = new ActionForward(ae.getPath());
} else {
forward = mapping.getInputForward();
}
//打印异常消息
ex.printStackTrace();
logger.error(ex.getMessage(), ex);
if(ex instanceof SystemException){
SystemException se = (SystemException)ex;
//取出key值
String key = se.getKey();
//创建异常消息
ActionMessage error = null;
if(key == null){
//ae.getKey() = errors.detail
error = new ActionMessage(ae.getKey(),se.getMessage());
}else{
error = new ActionMessage(key);
}
//将异常消息保存到request中
storeException(request, key, error, forward, "request");
//不再做处理,直接返回错误页面
return forward;
}
return super.execute(ex, ae, mapping, formInstance, request, response);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -