您的位置:首页 > 编程语言 > Java开发

struts异常的处理的一个例子

2009-09-02 17:02 477 查看
首先定义自己的异常类

Java代码



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

Java代码



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的配置文件上面配置上

Xml代码



<global-exceptions>

<exception

key="errors.detail"

type="java.lang.Exception"

path="/xxxx/exception.jsp"

scope="request"

handler="com.xxx.xxx.xxx.SystemExceptionHandler"

></exception>

</global-exceptions>

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


在程序中的使用

Java代码



if(xxx.getChildren().size()>0){

throw new SystemException("不能删除","langhua.error");

}

if(xxx.getChildren().size()>0){
throw new SystemException("不能删除","langhua.error");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: