首先定义自己的异常类
- 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;
- }
- }
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
- 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);
- }
- }
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的配置文件上面配置上
在程序中的使用
- if(xxx.getChildren().size()>0){
- throw new SystemException(string">"不能删除",string">"langhua.error");
- }
if(xxx.getChildren().size()>0){
throw new SystemException("不能删除","langhua.error");
}