`

统一异常处理

阅读更多
package com.bjsxt.oa.manager;

public class SystemException extends RuntimeException {

	//异常代码
	private String key;
	
	private Object[] values;
	
	public SystemException() {
		super();
	}

	public SystemException(String message, Throwable throwable) {
		super(message, throwable);
	}

	public SystemException(String message) {
		super(message);
	}

	public SystemException(Throwable throwable) {
		super(throwable);
	}
	
	public SystemException(String message,String key){
		super(message);
		this.key = key;
	}
	
	public SystemException(String message,String key,Object value){
		super(message);
		this.key = key;
		this.values = new Object[]{value};
	}
	
	public SystemException(String message,String key,Object[] values){
		super(message);
		this.key = key;
		this.values = values;
	}

	public String getKey() {
		return key;
	}

	public Object[] getValues() {
		return values;
	}

}


	<global-exceptions>
		<exception 
			key="errors.detail" 
			type="java.lang.Exception"
			path="/common/exception.jsp"
			scope="request"
			handler="com.bjsxt.oa.web.SystemExceptionHandler"
		></exception>
	</global-exceptions>

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.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.manager.SystemException;

public class SystemExceptionHandler extends ExceptionHandler {

	private static Log logger = LogFactory.getLog(SystemExceptionHandler.class);
	
	/**
	 * 处理SystemException异常
	 */
	@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();
		}
		
		logger.debug("出现异常", ex);
		
		//ex.printStackTrace();
		
		if(ex instanceof SystemException){
			SystemException se = (SystemException)ex;
			
			//取出key值
			String key = se.getKey();
			
			ActionMessage error = null;
			if( key == null){
				error = new ActionMessage(ae.getKey(),se.getMessage());
			}else{
				if(se.getValues() != null){
					error = new ActionMessage(key,se.getValues());
				}else{
					error = new ActionMessage(key);
				}
			}
			
			this.storeException(request, key, error, forward, ae.getScope());
			
			return forward;
		}
		
		
		return super.execute(ex, ae, mapping, formInstance, request, response);
	}

}


		if(org.getChildren().size() > 0){
			throw new SystemException("存在子机构,不允许删除","exception.org.del",org.getId());
		}
		
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics