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;
}
}
再定义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);
}
}
最后在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>
在程序中的使用
Java代码
if(xxx.getChildren().size()>0){
throw new SystemException("不能删除","langhua.error");
}
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"); }
相关文章推荐
- java面向对象-异常处理的一个例子
- 介绍一个专门处理C++异常的类和例子
- 关于处理try-catch循环一次异常,次次异常的一个例子
- Java的异常处理机制的一个例子-读后定能对异常理解深刻
- 关于Struts处理异常框架的小例子
- 关于Struts处理异常框架的小例子
- ASP.NET WebApi 上传文件时异常 Failed to execute send on XMLHttpRequest 的一个处理方法
- 异常处理方案的一个改进(兼论IE的一个局限性)
- 活动桌面处理和一个例子
- 5.11 一个用Spring AOP实现异常处理和记录程序执行时间的实例
- struts 2.3.15做例子时报异常:java.lang.RuntimeException: java.lang.reflect.InvocationTargetException 解决
- 异常的转换思想:当出现的异常是调用者处理不了的,就需要将此异常转换为一个调用者可以处理的异常抛出。
- 发生一个未处理的异常 脚本调试 错误号2912
- Struts FileUpload 异常处理之Processing of multipart/form-data request failed.
- [Struts]学习日记1 - 一个简单的例子
- .net下 存储过程、事务处理 的一个小例子
- 活动桌面处理和一个例子
- (六)Struts的简单异常处理
- java异常处理和自定义异常利用try和catch让程序继续下去(回来自己再写个例子试运行下)
- 处理了一个以前写的java小程序的异常