关于struts的异常处理
2009-11-01 08:37
645 查看
1,采用编程式(手工的异常处理),当捕获到异常的时候转到异常处理页面(要利用国际化资源文件)
代码(eg):
然后在那个异常处理的一面采用<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
异常类的代码:
异常处理类的代码:
代码(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); } }
相关文章推荐
- 关于struts项目中服务器启动出现异常的处理方式
- 关于Struts处理异常框架的小例子
- 关于Struts处理异常框架的小例子
- 关于Struts与Ajax整合时的异常处理
- 关于Struts与Ajax整合时的异常处理
- 关于异常和错误处理编程的精华文章。。来自Damien Katz
- 关于Response.redirect和Response.End出现线程中止异常的处理
- 关于Struts has detected an unhandled exception这个异常
- 关于SQLSERVER 事物的运用(3)嵌套事物以及嵌套存储过程的异常的处理
- C++之父Bjarne Stroustrup提供的关于异常处理的建议
- 一个关于用Struts实现文件上传异常的解决
- 关于 AppDomain 未知异常导致进程崩溃的处理办法
- 关于异常“The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine”的处理
- 关于wcf异常异常信息:由于内部错误,服务器无法处理该请求。有关该错误的详细信息,请打开服务器上的 IncludeExceptionDetailInFaults (从 ServiceBehaviorAttribut
- 关于异常“The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine”的处理
- Struts的异常处理
- 5.Strut国际化动态文本(声明式处理异常):struts-config.xml web.xml
- Java编程中关于异常处理的10个最佳实践
- 关于spring mybatis plus ssm应用报错的异常处理,找不到对应的bean对象
- 关于浏览器 显示400异常的处理办法