struts异常的处理的一个例子

news/2024/7/10 6:12:49 标签: struts, string, object, path, null, exception

 首先定义自己的异常类

Java代码 复制代码
  1. package com.langhua.oa.manager;   
  2.   
  3. public class SystemException extends RuntimeException {   
  4.     //key值   
  5.     private String key;   
  6.     //可扩展,提供多个构造方法   
  7.     private Object[] values;   
  8.            
  9.     public SystemException() {   
  10.         super();   
  11.     }   
  12.   
  13.     public SystemException(String message, Throwable cause) {   
  14.         super(message, cause);   
  15.     }   
  16.   
  17.     public SystemException(String message) {   
  18.         super(message);   
  19.     }   
  20.   
  21.     public SystemException(Throwable cause) {   
  22.         super(cause);   
  23.     }   
  24.     //自己定义的构造方法   
  25.     public SystemException(String message,String key){   
  26.         super(message);   
  27.         this.key = key;   
  28.     }   
  29.        
  30.     public SystemException(String message,String key,Object value){   
  31.         super(message);   
  32.         this.key = key;   
  33.         this.values = new Object[]{value};   
  34.     }   
  35.        
  36.     public SystemException(String message,String key,Object[] values){   
  37.         super(message);   
  38.         this.key = key;   
  39.         this.values = values;   
  40.     }   
  41.        
  42.     public String getKey() {   
  43.         return key;   
  44.     }   
  45.   
  46.     public Object[] getValues() {   
  47.         return values;   
  48.     }   
  49.   
  50. }  
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代码 复制代码
  1.   
  2. public class SystemExceptionHandler extends ExceptionHandler {   
  3.     //当发生异常的时候会自动调用下面的方法,并传过来里面的参数   
  4.     public ActionForward execute(   
  5.             Exception ex,    
  6.             ExceptionConfig ae,   
  7.             ActionMapping mapping,    
  8.             ActionForm formInstance,   
  9.             HttpServletRequest request,    
  10.             HttpServletResponse response)throws ServletException {   
  11.          //创建AcctionForward   
  12.         ActionForward forward = null;   
  13.         //从ExceptionConfig里面获得path 如path="/common/exception.jsp"   
  14.         if(ae.getPath() != null){   
  15.             //如果path不为空,就建立相当的forward   
  16.             forward = new ActionForward(ae.getPath());   
  17.         }else{   
  18.             //为空的话就使用默认的   
  19.             forward = mapping.getInputForward();   
  20.         }   
  21.            
  22.         //如果产生的异常是SystemException的一个实例   
  23.         if(ex instanceof SystemException){   
  24.             SystemException se = (SystemException)ex;   
  25.                
  26.             //取出key值   
  27.             String key = se.getKey();   
  28.             //根据相关的参数创建ActionMessage   
  29.             ActionMessage error = null;   
  30.             if( key == null){   
  31.                 error = new ActionMessage(ae.getKey(),se.getMessage());   
  32.             }else{   
  33.                 if(se.getValues() != null){   
  34.                     error = new ActionMessage(key,se.getValues());   
  35.                 }else{   
  36.                     error = new ActionMessage(key);   
  37.                 }   
  38.             }   
  39.             //是放到request里面还是放到session里面   
  40.             this.storeException(request, key, error, forward, ae.getScope());   
  41.             //带着参数传到相关的JSP页面   
  42.             return forward;   
  43.         }   
  44.         return super.execute(ex, ae, mapping, formInstance, request, response);   
  45.     }   
  46.        
  47. }  
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里面获得pathpath="/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代码 复制代码
  1. <global-exceptions>  
  2.         <exception    
  3.             key="errors.detail"    
  4.             type="java.lang.Exception"  
  5.             path="/xxxx/exception.jsp"  
  6.             scope="request"  
  7.             handler="com.xxx.xxx.xxx.SystemExceptionHandler"  
  8.         ></exception>  
  9.     </global-exceptions>  


在程序中的使用

Java代码 复制代码
  1. if(xxx.getChildren().size()>0){   
  2.     throw new SystemException(string">"不能删除",string">"langhua.error");   
  3. }     
if(xxx.getChildren().size()>0){
	throw new SystemException("不能删除","langhua.error");
}	

 


http://www.niftyadmin.cn/n/1051347.html

相关文章

C++ 格式化输出 及 输入 流

#include <iostream> #include <iomanip> using namespace std; void main() {int x1000;double y1.23456789;cout<<"默认x值:"<<x<<endl;cout<<"十进制:"<<dec<<x<<endl; …

ag 4.0 [记录]

页面绑定数据[disabled]"gCtrlIsCheck"[attr.data-h]el.id[src]el.url[class][class.c][style.color][style.background-image]"url(showListPics[0])" angularlayui.angular-cli.jsonstyles - "./assets/layui/css/layui.css"scripts - &q…

ConcurrentHashMap原理详解

参考链接&#xff1a;https://www.cnblogs.com/chengxiao/p/6842045.html https://www.cnblogs.com/ITtangtang/p/3948786.html 一、背景&#xff1a; 众所周知&#xff0c;哈希表是中非常高效&#xff0c;复杂度为O(1)的数据结构&#xff0c;在Java开发中&#xff0c;我们最常…

struts常见异常处理

1---:java.lang.NoClassDefFoundError: org/apache/struts2/dojo/views/jsp/ui/HeadTag 解决办法&#xff1a;原因缺少了dojo的JAR包&#xff0c;引入即可:struts2-dojo-plugin-2.1.2.jar ( The "head" tag renders required JavaScript code to configure Dojo a…

Leetcode 33 Search in Rotated Sorted Array

Leetcode 33 Search in Rotated Sorted Array 题目描述 Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]). You are given a target value to search. If found in…

软件测试和评估

计划说明 1.对比测试产品 百词斩、扇贝单词 2.测试进度表 项目 内容说明 预估耗时 &#xff08;分钟&#xff09; 实际耗时 &#xff08;分钟&#xff09; Planning 1.计划 5 5 Estimate 估计这个任务需要多少时间 5 5 Testing Design 2.测试设计 60 10…

pthread 学习笔记

windows和Linux的pthread用法稍有不同。 windows&#xff1a; 1.下载pthreads-w32-2-8-0-release.exe 下载地址&#xff1a;ftp://sourceware.org/pub/pthreads-win32 2. 安装pthreads-w32-2-8-0-release.exe 双击pthreads-w32-2-8-0-release.exe&#xff0c;点击Browse选择安…

js封装一个模块

(function(){ var defaultSetting { color:red } Setting (options) { var self this; self Object.assign(self, defaultSettings, options); } Setting.protoType { ... } window.setting Setting; })(window)转载于:https://www.cnblogs.com/Mrkaikai/p/9499488.html