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

关于struts的异常处理

2009-11-01 08:37 645 查看
1,采用编程式(手工的异常处理),当捕获到异常的时候转到异常处理页面(要利用国际化资源文件)

代码(eg):
try {

orgManager.del(orgId);

} catch(RuntimeException e){

ActionMessages msgs=new ActionMessages();

//其中e.getMessage()就是为了得到抛出的那个异常信息的内容

ActionMessage msg=new ActionMessage("errrors.detail",e.getMessage);

msgs.add("detail",msg);

this.saveErrors(request,msgs);

return mapping.findForword("exception");

//在Struts配置文件中指定exception所指定的页面

}


然后在那个异常处理的一面采用<html:errors />标签取出异常信息

2,采用统一的异常处理(将异常抛出)(struts的异常处理框架)

eg:

public ActionForward del(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {}

在配置文件中配置exception标签

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

其中handler表示处理异常的类

过程:

1,继承RuntimeException

2,实现父类的所有构造器

3,写一个handler类,继承ExceptionHander

4,覆写execute方法

5,要在国际化资源文件中加入key的值

抛出异常的句子为:

//其中errors.org.hassuborg是key,要和国际化资源文件中的key一致。

throw new SystemException("errors.org.hassuborg",new Object[]{org.getName(),org.getChildren().size()},"存在子机构信息,不允许删除");

在国际化文件中配置的属性为:

errors.org.hassuborg=Org[{0}] has {1} sub orgs

异常类的代码:

package com.bjsxt.oa.manager;

/**
* 继承父类的所有构造器
* @author Administrator
*
*/
public class SystemException extends RuntimeException {

//对应国际化资源文件中的那个key,作用就是用来区别每个异常
private String key;

/**
* 用来动态的存放一些数值
*/
private Object[] values;
public String getKey() {
return key;
}
public Object[] getValues() {
return 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 key,String message) {
super(message);
this.key = key;
}

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

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

}


异常处理类的代码:

package com.bjsxt.oa.web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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 {

@Override
public ActionForward execute(Exception ex, ExceptionConfig ae,
ActionMapping mapping, ActionForm formInstance,
HttpServletRequest request, HttpServletResponse response)
throws ServletException {
//
ActionForward forward = null;
ActionMessage error = null;

//根据path创建ActionForward
//当exception标签中有配置path属性的话,则根据这个path来创建
if (ae.getPath() != null) {
forward = new ActionForward(ae.getPath());
} else {
//如果没有配置path属性的话,则根据action中的input所指定的来创建ActionForward
forward = mapping.getInputForward();
}

//处理SystemException异常
//判断是否是SystemException,如果是的话,处理,如果不是的话,则交给父类去处理
if(ex instanceof SystemException){
SystemException se = (SystemException)ex;
//取出key值
String key = se.getKey();
if(key == null){
//如果key值是空的话,则去取得<exception>标签中的key值,然后创建一个异常消息
//key只可以通过ExceptionConfig取得key值
error = new ActionMessage(ae.getKey(),se.getMessage());
}else{
//当key值不为空的话,则根据key值创建消息文本,ActionMessage可以动态的改参数

//se中的value值是否为空,(也就是说是否带参数)
if(se.getValues() != null){
error = new ActionMessage(key,se.getValues());
}else{

error = new ActionMessage(key);
}
}

//将异常消息放入到request中
//forward表示转到什么地方去
this.storeException(request, key, error, forward, ae.getScope());

return forward;
}

return super.execute(ex, ae, mapping, formInstance, request, response);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: