struts异常的处理的一个例子

本文介绍如何在Java中自定义异常类SystemException及其处理程序SystemExceptionHandler。通过定义多种构造方法增强异常处理灵活性,实现异常信息的定制化显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 首先定义自己的异常类

Java代码 复制代码
  1. package com.langhua.oa.manager;   
  2.   
  3. public class SystemException extends RuntimeException {   
  4.     //key值   
  5.     private String key;   
  6.     //可扩展,提供多个构造方法   
  7.     private Object[] values;   
  8.            
  9.     public SystemException() {   
  10.         super();   
  11.     }   
  12.   
  13.     public SystemException(String message, Throwable cause) {   
  14.         super(message, cause);   
  15.     }   
  16.   
  17.     public SystemException(String message) {   
  18.         super(message);   
  19.     }   
  20.   
  21.     public SystemException(Throwable cause) {   
  22.         super(cause);   
  23.     }   
  24.     //自己定义的构造方法   
  25.     public SystemException(String message,String key){   
  26.         super(message);   
  27.         this.key = key;   
  28.     }   
  29.        
  30.     public SystemException(String message,String key,Object value){   
  31.         super(message);   
  32.         this.key = key;   
  33.         this.values = new Object[]{value};   
  34.     }   
  35.        
  36.     public SystemException(String message,String key,Object[] values){   
  37.         super(message);   
  38.         this.key = key;   
  39.         this.values = values;   
  40.     }   
  41.        
  42.     public String getKey() {   
  43.         return key;   
  44.     }   
  45.   
  46.     public Object[] getValues() {   
  47.         return values;   
  48.     }   
  49.   
  50. }  
package com.langhua.oa.manager;

public class SystemException extends RuntimeException {
	//key值
	private String key;
	//可扩展,提供多个构造方法
	private Object[] values;
		
	public SystemException() {
		super();
	}

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

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

	public SystemException(Throwable cause) {
		super(cause);
	}
	//自己定义的构造方法
	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;
	}

}



再定义SystemExceptionHandler extends ExceptionHandler

Java代码 复制代码
  1.   
  2. public class SystemExceptionHandler extends ExceptionHandler {   
  3.     //当发生异常的时候会自动调用下面的方法,并传过来里面的参数   
  4.     public ActionForward execute(   
  5.             Exception ex,    
  6.             ExceptionConfig ae,   
  7.             ActionMapping mapping,    
  8.             ActionForm formInstance,   
  9.             HttpServletRequest request,    
  10.             HttpServletResponse response)throws ServletException {   
  11.          //创建AcctionForward   
  12.         ActionForward forward = null;   
  13.         //从ExceptionConfig里面获得path 如path="/common/exception.jsp"   
  14.         if(ae.getPath() != null){   
  15.             //如果path不为空,就建立相当的forward   
  16.             forward = new ActionForward(ae.getPath());   
  17.         }else{   
  18.             //为空的话就使用默认的   
  19.             forward = mapping.getInputForward();   
  20.         }   
  21.            
  22.         //如果产生的异常是SystemException的一个实例   
  23.         if(ex instanceof SystemException){   
  24.             SystemException se = (SystemException)ex;   
  25.                
  26.             //取出key值   
  27.             String key = se.getKey();   
  28.             //根据相关的参数创建ActionMessage   
  29.             ActionMessage error = null;   
  30.             if( key == null){   
  31.                 error = new ActionMessage(ae.getKey(),se.getMessage());   
  32.             }else{   
  33.                 if(se.getValues() != null){   
  34.                     error = new ActionMessage(key,se.getValues());   
  35.                 }else{   
  36.                     error = new ActionMessage(key);   
  37.                 }   
  38.             }   
  39.             //是放到request里面还是放到session里面   
  40.             this.storeException(request, key, error, forward, ae.getScope());   
  41.             //带着参数传到相关的JSP页面   
  42.             return forward;   
  43.         }   
  44.         return super.execute(ex, ae, mapping, formInstance, request, response);   
  45.     }   
  46.        
  47. }  
public class SystemExceptionHandler extends ExceptionHandler {
	//当发生异常的时候会自动调用下面的方法,并传过来里面的参数
	public ActionForward execute(
			Exception ex, 
			ExceptionConfig ae,
			ActionMapping mapping, 
			ActionForm formInstance,
			HttpServletRequest request, 
			HttpServletResponse response)throws ServletException {
		 //创建AcctionForward
		ActionForward forward = null;
		//从ExceptionConfig里面获得path 如path="/common/exception.jsp"
		if(ae.getPath() != null){
			//如果path不为空,就建立相当的forward
			forward = new ActionForward(ae.getPath());
		}else{
			//为空的话就使用默认的
			forward = mapping.getInputForward();
		}
		
		//如果产生的异常是SystemException的一个实例
		if(ex instanceof SystemException){
			SystemException se = (SystemException)ex;
			
			//取出key值
			String key = se.getKey();
			//根据相关的参数创建ActionMessage
			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);
				}
			}
			//是放到request里面还是放到session里面
			this.storeException(request, key, error, forward, ae.getScope());
			//带着参数传到相关的JSP页面
			return forward;
		}
		return super.execute(ex, ae, mapping, formInstance, request, response);
	}
	
}



最后在struts的配置文件上面配置上

Xml代码 复制代码
  1. <global-exceptions>  
  2.         <exception    
  3.             key="errors.detail"    
  4.             type="java.lang.Exception"  
  5.             path="/xxxx/exception.jsp"  
  6.             scope="request"  
  7.             handler="com.xxx.xxx.xxx.SystemExceptionHandler"  
  8.         ></exception>  
  9.     </global-exceptions>  


在程序中的使用

Java代码 复制代码
  1. if(xxx.getChildren().size()>0){   
  2.     throw new SystemException("不能删除","langhua.error");   
  3. }     
if(xxx.getChildren().size()>0){
	throw new SystemException("不能删除","langhua.error");
}	

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值