![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
struts2拦截器 怎么写能捕获到程序因为数据库里的字段为null ,程序在后台报的空指针异常??? 在线等
我这里主要是想捕获异常发送指定的邮箱功能struts.xml配置文件:<interceptors><!--定义权限控制拦截器--><interceptorname="au...
我这里主要是想捕获异常发送指定的邮箱功能
struts.xml 配置文件:
<interceptors>
<!-- 定义权限控制拦截器 -->
<interceptor name="authority" class="com.wxh.util.AuthorizationInterceptor" />
<!-- 定义一个包含权限控制的拦截器栈 -->
<interceptor-stack name="mydefault">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="authority" />
</interceptor-stack>
</interceptors>
<!-- 定义默认拦截器 -->
<default-interceptor-ref name="mydefault" />
<global-results>
<result name="error">err.jsp</result>
<result name="login">login.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
java类
public class AuthorizationInterceptor extends AbstractInterceptor {
private String rtAction;
/*
* 拦截Action处理的拦截方法
*
*/
public String intercept(ActionInvocation invocation) throws Exception {
ExceptionMail exceptionMail=new ExceptionMail();
try
{
ActionContext ctx = invocation.getInvocationContext();
Map session = ctx.getSession();
Action action = (Action)invocation.getAction();
if (action instanceof LoginAction || action instanceof PnrTelPayAction) {
return invocation.invoke();
}
Long userName = (Long)session.get("loginId");
if(userName == null)
{
rtAction= Action.LOGIN;
} else {
rtAction= invocation.invoke();
}
}catch (Exception e)
{
//调用邮件方法传参数
exceptionMail.sendException(e.getCause().getMessage()+"\r\n"+e.toString());
e.printStackTrace();
rtAction= Action.ERROR;
}
return rtAction;
}
}
现在我有个异常 是空指针异常,为什么拦截器捕获不到呢?异常如下:
java.lang.NullPointerException
at com.wxh.service.impl.ReturnOrderServiceImpl.getHistoryDetailOrder(ReturnOrderServiceImpl.java:305)
at com.wxh.action.OrderAction.orderInfo(OrderAction.java:695)
at com.wxh.action.OrderAction.execute(OrderAction.java:227)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke 展开
struts.xml 配置文件:
<interceptors>
<!-- 定义权限控制拦截器 -->
<interceptor name="authority" class="com.wxh.util.AuthorizationInterceptor" />
<!-- 定义一个包含权限控制的拦截器栈 -->
<interceptor-stack name="mydefault">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="authority" />
</interceptor-stack>
</interceptors>
<!-- 定义默认拦截器 -->
<default-interceptor-ref name="mydefault" />
<global-results>
<result name="error">err.jsp</result>
<result name="login">login.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
java类
public class AuthorizationInterceptor extends AbstractInterceptor {
private String rtAction;
/*
* 拦截Action处理的拦截方法
*
*/
public String intercept(ActionInvocation invocation) throws Exception {
ExceptionMail exceptionMail=new ExceptionMail();
try
{
ActionContext ctx = invocation.getInvocationContext();
Map session = ctx.getSession();
Action action = (Action)invocation.getAction();
if (action instanceof LoginAction || action instanceof PnrTelPayAction) {
return invocation.invoke();
}
Long userName = (Long)session.get("loginId");
if(userName == null)
{
rtAction= Action.LOGIN;
} else {
rtAction= invocation.invoke();
}
}catch (Exception e)
{
//调用邮件方法传参数
exceptionMail.sendException(e.getCause().getMessage()+"\r\n"+e.toString());
e.printStackTrace();
rtAction= Action.ERROR;
}
return rtAction;
}
}
现在我有个异常 是空指针异常,为什么拦截器捕获不到呢?异常如下:
java.lang.NullPointerException
at com.wxh.service.impl.ReturnOrderServiceImpl.getHistoryDetailOrder(ReturnOrderServiceImpl.java:305)
at com.wxh.action.OrderAction.orderInfo(OrderAction.java:695)
at com.wxh.action.OrderAction.execute(OrderAction.java:227)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke 展开
2个回答
展开全部
在dao层和service层将异常捕获到向外抛出,action调用的时候捕获该异常进行处理。
追问
能提示一下,具体抛出的例子吗?代码... ....
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询