struts2异常拦截器 global-results不起作用

longge424 2011-03-21 02:55:29
配置文件: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

请大家帮忙分析一下!谢谢。
...全文
1446 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
z_0216 2013-11-07
  • 打赏
  • 举报
回复
我也遇到了同样的问题,解决方法就是<exception-mapping result="bbb" exception="java.lang.Exception" /> 中的result名称和<global-results><result name="bbb">/default.jsp</result></global-results>中name一致。但是配置全局的result一定要一起设置<global-exception-mappings>吗,为什么我单独设置全局result总是出现异常
angelfish 2012-07-20
  • 打赏
  • 举报
回复
我也遇到同样问题,解决办法是把result="error"换成其它的,如result = "aaa",问题就解决了。
meiguiyuan_ygm 2011-10-13
  • 打赏
  • 举报
回复
学习了
sxcdbei 2011-07-06
  • 打赏
  • 举报
回复
你这个问题解决了吗?是这么解决的,我刚好也碰到了这个问题
longge424 2011-03-22
  • 打赏
  • 举报
回复
您好,谢谢你的回答。
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();

}

}

这是这两个类
redlotus_lyn 2011-03-21
  • 打赏
  • 举报
回复
exceptionManager异常应该是异常的一种类型。


不用在commonInterceptor拦截器栈中引用,

因为defaultStack中异常处理的拦截器exception

如果业务中抛出exceptionManager异常,exception拦截器会处理转发到<global-exception-mappings>
中的result的。
redlotus_lyn 2011-03-21
  • 打赏
  • 举报
回复
继承Exception看看。

public class SystemException extends Exception
拦截器和过滤器的区别 1、拦截器基于动态代理 , 过滤器基于函数回调 2、拦截器不依赖于servlet容器,通过动态代理实现,过滤器依赖于servlet容器 3、拦截器在方法前后,异常前后等调用,而过滤器只能在请求前和请求后各调一次。 4、拦截器可以利用依赖注入,因此在spring框架程序中,优先拦截器 5、拦截器是包裹在过滤器中使用的。 复习 converter 转换器 i18n struts2 spring MVC 拦截器 interceptor 过滤器 filter web.xml implements filter filterchain arg2.doFilter(req,resp); 监听器 servlet application /session /request 6/8 个 1、拦截器 定义拦截器的包 定义拦截器的核心 定义拦截器 定义拦截器的值栈空间 引入定义的拦截器 拦截错误跳转的页面 <global-results> <result name="error">/demo01/error.jspresult> global-results> 2、 token <result name="invalid.token">/demo02/error.jspresult> <%@ taglib uri="/struts-tags" prefix="s" %> 3、500字struts2的科学说明文 每人一份 2018-03-19 下午演讲(脱稿) 4、小结 1、struts2对servlet封装(request,response) ,资源调配和资源的映射 2、框架设计的思想 istruts 配置,过滤器,反射 istruts.properties 3、starts2的使用思路 1、jar 2、配置文件 3、常用类 servletActionContext 4、访问流程 url -> filter -> struts.xml -> package -> action -> name="userAction_*" -> class -> method="{1}" 5、核心struts.xml 6、提交策略 图片预览 个人信息完善 7、crud 增删改查 搜索 多删 假删 ids ->action -> dao PreparedStatement -- "delete from user where id in ("+ids+")"; 8、converter i18n ModeDriven stack 9、interceptor token 5、hibernate 4.1.4 搭建架构 ORM --- Object relational Mapping UserBean userBean.hbm.xml db table let java objects hibernate in the relational datebase ! persistent Class

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧