配置文件:struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"><struts>
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="i18n/Messages" />
<package name="crud-default" extends="struts-default" namespace="/">
<interceptors>
<interceptor name="exceptionManager" class="com.igames.fourd.aop.ExceptionInterceptor" />
<!-- 异常拦截器的拦截栈 -->
<interceptor-stack name="commonInterceptor">
<interceptor-ref name="exceptionManager" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<!-- 设置默认拦截器 -->
<default-interceptor-ref name="commonInterceptor" /> <global-results>
<result name="error">/commons/exception.jsp</result>
</global-results> <global-exception-mappings>
<exception-mapping result="error" exception="com.igames.fourd.exception.SystemException" />
<!-- <exception-mapping result="error" exception="java.lang.Exception" />-->
</global-exception-mappings>
</package>
<include file="struts/struts-system.xml" />
</struts>如果将<global-exception-mappings>中改为<exception-mapping result="error" exception="java.lang.Exception" />则能够进入到全局映射的result。SystemException的定义为
public class SystemException extends RuntimeException请大家帮忙分析一下!谢谢。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"><struts>
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="i18n/Messages" />
<package name="crud-default" extends="struts-default" namespace="/">
<interceptors>
<interceptor name="exceptionManager" class="com.igames.fourd.aop.ExceptionInterceptor" />
<!-- 异常拦截器的拦截栈 -->
<interceptor-stack name="commonInterceptor">
<interceptor-ref name="exceptionManager" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<!-- 设置默认拦截器 -->
<default-interceptor-ref name="commonInterceptor" /> <global-results>
<result name="error">/commons/exception.jsp</result>
</global-results> <global-exception-mappings>
<exception-mapping result="error" exception="com.igames.fourd.exception.SystemException" />
<!-- <exception-mapping result="error" exception="java.lang.Exception" />-->
</global-exception-mappings>
</package>
<include file="struts/struts-system.xml" />
</struts>如果将<global-exception-mappings>中改为<exception-mapping result="error" exception="java.lang.Exception" />则能够进入到全局映射的result。SystemException的定义为
public class SystemException extends RuntimeException请大家帮忙分析一下!谢谢。
不用在commonInterceptor拦截器栈中引用,因为defaultStack中异常处理的拦截器exception如果业务中抛出exceptionManager异常,exception拦截器会处理转发到<global-exception-mappings>
中的result的。
public class SystemException extends Exception 继承Exception也不行 我写SystemException是为了对错误进行友好的封装返回到前台 public class ExceptionInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = 1L; @Override
public String intercept(ActionInvocation invocation) throws Exception {
String result = "";
try {
result = invocation.invoke();
} catch (DataAccessException ex) {
ex.printStackTrace();
throw new SystemException("数据库操作失败,");
} catch (NullPointerException ex) {
ex.printStackTrace();
throw new SystemException("调用了未经初始化的对象或者是不存在的对象,");
} catch (IOException ex) {
ex.printStackTrace();
throw new SystemException("IO异常,");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
throw new SystemException("指定的类不存在,");
} catch (ArithmeticException ex) {
ex.printStackTrace();
throw new SystemException("数学运算异常,");
} catch (ArrayIndexOutOfBoundsException ex) {
ex.printStackTrace();
throw new SystemException("数组下标越界,");
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
throw new SystemException("方法的参数错误,");
} catch (MailException ex) {
ex.printStackTrace();
throw new SystemException("邮件发送出现异常,");
} catch (ClassCastException ex) {
ex.printStackTrace();
throw new SystemException("类型强制转换错误,");
} catch (SQLException ex) {
ex.printStackTrace();
throw new SystemException("操作数据库异常,");
} catch (NoSuchMethodException ex) {
ex.printStackTrace();
throw new SystemException("方法末找到异常,");
} catch (Exception ex) {
ex.printStackTrace();
throw new SystemException("程序内部错误,操作失败,");
}
return result;
}
}public class SystemException extends Exception {
private static final long serialVersionUID = 0xc1a865c45ffdc5f9L; public SystemException(String frdMessage){
super(createFriendlyErrMsg(frdMessage));
} public SystemException(Throwable throwable){
super(throwable);
} public SystemException(Throwable throwable, String frdMessage){
super(throwable);
} private static String createFriendlyErrMsg(String msgBody) { String prefixStr = "抱歉,";
String suffixStr = " 请稍后再试或与管理员联系!";
StringBuffer friendlyErrMsg = new StringBuffer("");
friendlyErrMsg.append(prefixStr);
friendlyErrMsg.append(msgBody);
friendlyErrMsg.append(suffixStr);
return friendlyErrMsg.toString(); }}这是这两个类