使用struts2拦截器与struts自定义异常构建自定义提示信息

1.我写这段代码实现的愿望,ssh验证提示使用的不是很顺手,可能是我用的不太熟悉吧,最后我考虑了一下自己的SSH消息提示信息。主要是通过struts2中的自定义异常与strut2的拦截器实现消息提示。

2.拦截器中分为两种异常一种为系统中已经定义好的异常,处理方式为直接转跳到一个固定页面进行友好提示,第二种方式为自定义系统中的消息提示。如下提出代码:

自定义异常类:

package com.finance.exception;

import com.finance.util.ApplicationContextUtils;
import com.finance.util.PropertiesUtils;

/**
 * 请求运行时异常
 * 
 * @author guyw
 *
 * 2014-3-9
 */
public class AppContextException extends RuntimeException{
	
	/**
	 * 根据状态判断错误
	 * @param sate 错误状态
	 * @param message 错误信息
	 */
	public AppContextException(String state,String message) {
//		PropertiesUtils propertiesUtils=(PropertiesUtils) ApplicationContextUtils.getApplicationContext().getBean("propertiesUtils");
//		String errorMessage=(String) propertiesUtils.getProperties().get(state);
		super(state+":"+message);
	}
	/**
	 * 根据状态判断错误
	 * @param sate 错误状态
	 */
	public AppContextException(String state) {
//		PropertiesUtils propertiesUtils=(PropertiesUtils) ApplicationContextUtils.getApplicationContext().getBean("propertiesUtils");
//		String errorMessage=(String)propertiesUtils.getProperties().get(state);
		super(state);		
	}
}

  自定义:拦截器

 

package com.finance.intercepter;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.dao.DataAccessException;

import com.finance.exception.AppContextException;
import com.finance.util.ApplicationContextUtils;
import com.finance.util.PropertiesUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

/**
 * 
 * 全局异常拦截器
 * 
 * @author guyw
 * 
 *         2014-3-9
 */
public class AppContextExceptionIntercepter implements Interceptor {
 
	private static final long serialVersionUID = 1L;
	
	private String errorMessage;
	@Override
	public void destroy() {

	}

	@Override
	public void init() {
		
	}

	@Override
	public String intercept(ActionInvocation invocation) {
		ActionContext ctx = invocation.getInvocationContext();  
        HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
        String result=null;
		try {
			  result=invocation.invoke();
		} catch (DataAccessException ex) {
			throw new AppContextException("10001", "数据库操作失败!");
		} catch (NullPointerException ex) {
			throw new AppContextException("10001", "调用了未经初始化的对象或者是不存在的对象!");
		} catch (IOException ex) {
			throw new AppContextException("10001", "IO异常!");
		} catch (ClassNotFoundException ex) {
			throw new AppContextException("10001", "指定的类不存在!");
		} catch (ArithmeticException ex) {
			throw new AppContextException("10001", "数学运算异常!");
		} catch (ArrayIndexOutOfBoundsException ex) {
			throw new AppContextException("10001", "数组下标越界!");
		} catch (IllegalArgumentException ex) {
			throw new AppContextException("10001", "方法的参数错误!");
		} catch (ClassCastException ex) {
			throw new AppContextException("10001", "类型强制转换错误!");
		} catch (SecurityException ex) {
			throw new AppContextException("10001", "违背安全原则异常!");
		} catch (SQLException ex) {
			throw new AppContextException("10001", "操作数据库异常!");
		} catch (NoSuchMethodError ex) {
			throw new AppContextException("10001", "方法末找到异常!");
		} catch (InternalError ex) {
			throw new AppContextException("10001", "Java虚拟机发生了内部错误");
		}
		catch (AppContextException ex) {
			PropertiesUtils propertiesUtils = (PropertiesUtils) ApplicationContextUtils.getApplicationContext().getBean("propertiesUtils");
			String state=ex.toString();
			String message=(String) propertiesUtils.getProperties().get(state);
			if(errorMessage!=null){
				errorMessage=state+","+message;
			}else{
				errorMessage=state;
			}
			request.setAttribute("errorMessage",errorMessage);
			return "success";
		}
		catch (Exception ex) {
			throw new AppContextException("10001", "程序内部错误,操作失败!");
		}
		
		return "exceptionErrorMessage";
	}
}

 如上红色代码 return "success"; 此处有一个疑问 如果在Action中抛出异常后拦截器捕获后返回结果集为 404错误,不知道改则么处理:错误页面代码:

HTTP Status 404 - result 'null' not found


type Status report

message result 'null' not found

description The requested resource (result 'null' not found) is not available.


Apache Tomcat/6.0.13

 PropertiesUtils 为一些自定义的消息固定提示信息key-value 比如:10001=登录失败!。

使用方式为 此处有一个上传功能:

/**
	 * 保存合作机构
	 * @return
	 */
	public String saveOrg(){
		if(true){
			
			throw new AppContextException("10001", "上传文件错误!");
		}
		return SUCCESS;
	}

 这样就能构建全局的消息提示信息。新手发帖赶紧喷吧。

请教问题:如上红色代码 return "success"; 此处有一个疑问 如果在Action中抛出异常后拦截器捕获后返回结果集为 404错误,不知道改则么处理,是不是如果在Action抛出异常后,在拦截器中返回的结果集与Action的拦截器就无关了?

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值