package cn.com.css.common.interceptor;
import java.io.ioexception;
import java.sql.sqlexception;
import org.apache.commons.logging.log;
import org.apache.commons.logging.logfactory;
import org.springframework.dao.dataaccessexception;
import cn.com.css.common.exception.msipsysexception;
import cn.com.css.util.sysconstants;
import com.opensymphony.xwork2.actioninvocation;
import com.opensymphony.xwork2.interceptor.abstractinterceptor;
/**
* 此类是struts2的拦截器当发生错误时<br/>
* 由此类进行拦截并调用系统的异常处理类<br/>
* 生成页面错误信息和打印log日志<br/>
*
* @version 1.0
*/
@suppresswarnings("serial")
public class msipsysexceptioninterceptor extends abstractinterceptor {
//log4j实例名
private static final log errorlog = logfactory.getlog(sysconstants.log_error);
//构造方法
private msipsysexceptioninterceptor() {
}
//初始化realpath只在服务器启动时执行一次
@override
public void init() {
//主拦截方法,拦截常用的错误异常,当有异常发生时进行捕捉并打印log日志和生成页面的错误提示信息
public string intercept(actioninvocation invocation) throws exception {
string method=invocation.getproxy().getmethod();
string namespace = invocation.getproxy().getnamespace().substring(invocation.getproxy().getnamespace().lastindexof("/")+1);
string result = "";
string mes = ""
+ "<br><br>namespace:" + invocation.getproxy().getnamespace()
+ "<br>action:" + invocation.getproxy().getaction()
+ "<br>method:" + invocation.getproxy().getmethod()
+ "<br><br>" + "具体的错误信息为:";
try {
result = invocation.invoke();
} catch (dataaccessexception ex) {
this.logging(method, namespace, ex);
invocation.getinvocationcontext().put("invocationerror","数据库操作失败!" );
throw new msipsysexception(mes+ ex.getmessage());
} catch (nullpointerexception ex) {
invocation.getinvocationcontext().put("invocationerror","调用了未经初始化的对象或者是不存在的对象!" );
throw new msipsysexception(mes + ex.getmessage());
} catch (ioexception ex) {
invocation.getinvocationcontext().put("invocationerror","io异常!" );
} catch (classnotfoundexception ex) {
invocation.getinvocationcontext().put("invocationerror", "指定的类不存在!" );
} catch (arithmeticexception ex) {
invocation.getinvocationcontext().put("invocationerror", "数学运算异常!");
} catch (arrayindexoutofboundsexception ex) {
invocation.getinvocationcontext().put("invocationerror", "数组下标越界!" );
} catch (illegalargumentexception ex) {
invocation.getinvocationcontext().put("invocationerror", "方法的参数错误!");
} catch (classcastexception ex) {
invocation.getinvocationcontext().put("invocationerror","类型强制转换错误!");
} catch (securityexception ex) {
invocation.getinvocationcontext().put("invocationerror","违背安全原则异常!");
} catch (sqlexception ex) {
invocation.getinvocationcontext().put("invocationerror","操作数据库异常!" );
} catch (nosuchmethoderror ex) {
invocation.getinvocationcontext().put("invocationerror","方法末找到异常!" );
} catch (internalerror ex) {
invocation.getinvocationcontext().put("invocationerror","java虚拟机发生了内部错误" );
} catch (exception ex) {
invocation.getinvocationcontext().put("invocationerror","程序内部错误,操作失败!" );
}
return result;
//打印log日志
private void logging(string method,string namespace,exception ex)
{
errorlog.debug("-------------------"+namespace+"提交的method="+method+"---------------\n");
errorlog.fatal("错误提示:"+ex.getmessage(),ex);
} catch (exception e) {
errorlog.debug("-------------------loggingmeg.xml文件解析错误---------------\n");
private void logging(string method,string namespace,nosuchmethoderror ex)
private void logging(string method,string namespace,internalerror ex)
}