Struts2异常处理实例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2异常处理实例相关的知识,希望对你有一定的参考价值。
一 介绍
java ee项目在某些情况下难免会产生一些异常,如果某些异常我们没有进行处理,那么将会抛给服务器,最后服务器会将异常信息直接在前台页面打印出来,比如说这样:
像这种直接将异常打印出来是非常不好的,不仅因为普通用户看不懂,而且还会将服务器的一些内部信息暴露出来,比如说:绝对路径,这样可能将会“方便”某些黑客进行非法入侵。因此我们需要对这些异常进行处理,给用户显示一个比较友好的界面。
二 简单处理
我们可以通过struts2的全局异常处理机制来处理,这种比较简单,配置struts.xml文件,然后添加异常页面即可。
(1)配置struts.xml :
在package里添加:
<global-results> <result name="error">/error.html</result> </global-results> <global-exception-mappings> <exception-mapping result="error" exception="java.lang.Exception"></exception-mapping> </global-exception-mappings>
比如说我的是这样的:
<package name="bid" namespace="/bid" extends="struts-default"> <global-results> <result name="error">/error.html</result> </global-results> <global-exception-mappings> <exception-mapping result="error" exception="java.lang.Exception"></exception-mapping> </global-exception-mappings> <action name="doSubmitBid" class="bid" method="doSubmitBid"> <result name="index" type="redirect">/index.jsp</result> </action> <action name="doDeal" class="bid" method="doDeal"> <result name="index" type="redirect">/index.jsp</result> </action> </package>
(2)准备一个error页面——error.html:
这个就随便了,比如说我的是这样的:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>出现异常啦</title> <style type="text/css"> body { background-image:url(‘/OnlineAuction/images/404.jpg‘); background-repeat:no-repeat; background-position:50% 55px; background-attachment:fixed; } </style> </head> <body> <div align="center" style="color:red;font-size:24px;font-weight:bold;position: relative;top:10px;">出现异常啦,请联系管理员或稍后再试!</div> </body> </html>
(3)效果:
以上配置好以后,再次出现异常时就会这样显示了:
可以看出,现在的异常页面就比刚才友好多了
三 使用拦截器处理异常
使用这种方式就比上面的稍微多了一点步骤,不过提示的信息也要具体些,具体的步骤如下:
(1)在struts.xml文件里的package里定义拦截器:
<interceptors> <!-- 声明拦截器 --> <interceptor name="errorInterceptor" class="com.zxpm.interceptor.ErrorInterceptor" /> <!-- 配置拦截器栈 --> <interceptor-stack name="myErrorInterceptor"> <interceptor-ref name="defaultStack" /> <interceptor-ref name="errorInterceptor" /> </interceptor-stack> </interceptors> <!-- 覆盖底层的拦截器栈 对包中的所有action都有效 --> <default-interceptor-ref name="myErrorInterceptor" /> <global-results> <result name="errorMsg">/error.jsp</result> </global-results> <global-exception-mappings> <exception-mapping result="errorMsg" exception="java.lang.Exception"></exception-mapping> </global-exception-mappings>
(2)com.zxpm.interceptor.ErrorInterceptor.java对异常进行处理:
package com.zxpm.interceptor; import java.io.IOException; import java.sql.SQLException; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.StrutsStatics; import org.springframework.dao.DataAccessException; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class ErrorInterceptor implements Interceptor { public String intercept(ActionInvocation arg0) throws Exception { try { String result = arg0.invoke(); // Action的返回值 return result; } catch (DataAccessException ex) { HttpServletRequest request = (HttpServletRequest) arg0.getInvocationContext() .get(StrutsStatics.HTTP_REQUEST); request.setAttribute("errorMsg", "数据库操作失败!"); return "errorMsg"; } catch (NullPointerException ex) { HttpServletRequest request = (HttpServletRequest) arg0.getInvocationContext() .get(StrutsStatics.HTTP_REQUEST); request.setAttribute("errorMsg", "空指针异常!"); return "errorMsg"; } catch (IOException ex) { HttpServletRequest request = (HttpServletRequest) arg0.getInvocationContext() .get(StrutsStatics.HTTP_REQUEST); request.setAttribute("errorMsg", "IO读写异常!"); return "errorMsg"; } catch (ClassNotFoundException ex) { HttpServletRequest request = (HttpServletRequest) arg0.getInvocationContext() .get(StrutsStatics.HTTP_REQUEST); request.setAttribute("errorMsg", "指定的类不存在!"); return "errorMsg"; } catch (ArithmeticException ex) { HttpServletRequest request = (HttpServletRequest) arg0.getInvocationContext() .get(StrutsStatics.HTTP_REQUEST); request.setAttribute("errorMsg", "数学运算异常!"); return "errorMsg"; } catch (ArrayIndexOutOfBoundsException ex) { HttpServletRequest request = (HttpServletRequest) arg0.getInvocationContext() .get(StrutsStatics.HTTP_REQUEST); request.setAttribute("errorMsg", "数组下标越界!"); return "errorMsg"; } catch (IllegalArgumentException ex) { HttpServletRequest request = (HttpServletRequest) arg0.getInvocationContext() .get(StrutsStatics.HTTP_REQUEST); request.setAttribute("errorMsg", "调用方法的参数错误!"); return "errorMsg"; } catch (ClassCastException ex) { HttpServletRequest request = (HttpServletRequest) arg0.getInvocationContext() .get(StrutsStatics.HTTP_REQUEST); request.setAttribute("errorMsg", "类型强制转换错误!"); return "errorMsg"; } catch (SecurityException ex) { HttpServletRequest request = (HttpServletRequest) arg0.getInvocationContext() .get(StrutsStatics.HTTP_REQUEST); request.setAttribute("errorMsg", "违背安全原则异常!"); return "errorMsg"; } catch (SQLException ex) { HttpServletRequest request = (HttpServletRequest) arg0.getInvocationContext() .get(StrutsStatics.HTTP_REQUEST); request.setAttribute("errorMsg", "数据库操作异常!"); return "errorMsg"; } catch (NoSuchMethodError ex) { HttpServletRequest request = (HttpServletRequest) arg0.getInvocationContext() .get(StrutsStatics.HTTP_REQUEST); request.setAttribute("errorMsg", "调用了未定义的方法!"); return "errorMsg"; } catch (InternalError ex) { HttpServletRequest request = (HttpServletRequest) arg0.getInvocationContext() .get(StrutsStatics.HTTP_REQUEST); request.setAttribute("errorMsg", "Java虚拟机发生了内部错误!"); return "errorMsg"; } catch (Exception ex) { HttpServletRequest request = (HttpServletRequest) arg0.getInvocationContext() .get(StrutsStatics.HTTP_REQUEST); request.setAttribute("errorMsg", "Sorry,系统发生了未知的错误,请联系管理员或稍后再试!"); return "errorMsg"; } } public void destroy() { } public void init() { } }
(3)前台友好提示页面:
这里跟前面的差不多,多了一点对定义好的常见异常的提示,代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE > <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>出现异常啦</title> <style type="text/css"> body { background-image: url(‘/OnlineAuction/images/404.jpg‘); background-repeat: no-repeat; background-position: 50% 55px; background-attachment: fixed; } </style> </head> <body> <div align="center" style="color: red; font-size: 24px; font-weight: bold; position: relative; top: 10px;"> <s:if test="#request.errorMsg !=null"> ${requestScope.errorMsg} </s:if> <s:else> 系统发生未知异常,请联系管理员或稍后再试! </s:else> </div> </body> </html>
(4)测试:
为了测试效果,我手动抛出了一个空指针异常:
然后进行测试,显示效果如下:
对于未定义的异常,显示的效果是这样的:
本文出自 “zifangsky的个人博客” 博客,请务必保留此出处http://983836259.blog.51cto.com/7311475/1741498
以上是关于Struts2异常处理实例的主要内容,如果未能解决你的问题,请参考以下文章