纳税服务系统【异常处理、抽取BaseAction】
2017-05-26 18:15
561 查看
前言
本博文主要讲解在项目中异常是怎么处理的。一般我们都不会直接把后台异常信息返回给用户,用户是看不懂的。让用户看见一大串的错误代码,这是不合理的。因此我们需要对报错进行处理。我们在开发的时候是使用层次来进行开发的。因此有三个层次:
① Action层可能出现解析请求参数、返回结果有问题;
dao【如果在这里报错了,一般都是比较致命的,我们先不管】
② Service 层则可能出现请求中要做的业务操作出现问题;出现了问题要根据实际情况判断是否会影响本次操作结果,action中要根据异常信息进行判断然后确定是否操作成功;
service【service层需要我们自定义异常】
③ dao层也可能出现在操作数据库时出现错误;而此种错误一般都是致命的会影响操作结果。
action【Action层也需要我们自定义异常】
因此;在3个层次中至少要有两种类型的异常信息来标识。
异常类的定义应该放在core核心模块的exception包下的。
自定义异常类
总的系统异常类
/**** * 这是我们自定义的总系统异常类 * * */ public class SysException extends Exception { //用来记录错误的信息! private String errorMsg; public String getErrorMsg() { return errorMsg; } public void setErrorMsg(String errorMsg) { this.errorMsg = errorMsg; } public SysException() { } public SysException(String message) { super(message); this.errorMsg= message; } public SysException(String message, Throwable cause) { super(message, cause); this.errorMsg= message; } public SysException(Throwable cause) { super(cause); } }
Action异常类
继承着我们自定义的总系统异常类 /** * Action的异常类 * */ public class ActionException extends SysException { public ActionException() { super("请求操作失败了!"); } public ActionException(String message) { super(message); } }
Service异常类
/** * Created by ozc on 2017/5/26. */ public class ServiceException extends SysException { public ServiceException() { super("操作业务失败了!"); } public ServiceException(String message) { super(message); } }
全局异常映射
我们使用的是Struts2框架,想要报错的信息不直接给用户看见。就在Struts总配置文件中配置对应的映射。<!-- 配置全局结果及异常映射 --> <package name="base-default" extends="struts-default"> <!-- 全局返回结果 --> <global-results> <!--这是我们自定义异常的错误--> <result name="sysError">/WEB-INF/jsp/error.jsp</result> <!--这是找不着映射路径的错误--> <result name="input">/WEB-INF/jsp/error.jsp</result> </global-results> <!-- 全局异常映射 --> <global-exception-mappings> <exception-mapping result="sysError" exception="zhongfucheng.core.exception.SysException"></exception-mapping> <exception-mapping result="input" exception="java.lang.Exception"></exception-mapping> </global-exception-mappings> </package>
应用
在子模块中,只要继承着我配置异常信息的package就行了。Serive层抛出异常:
@Override public List<User> findObjects() throws ServiceException { try { int i = 1 / 0; } catch (Exception e) { throw new ServiceException(e.getMessage()); } return userDaoImpl.findObjects(); }
Action层把它catch住,并抛出Action异常:
//抛出Action异常 public String listUI() throws ActionException { try { userList = userServiceImpl.findObjects(); } catch (ServiceException e) { throw new ActionException("请求操作失败!!!" + e.getMessage()); } return "listUI"; }
即使Action中出现了ActionExcpetion以外的异常,我们在Struts配置文件中已经配置了Exception了。还是可以将它捕获得到
error.jsp页面
<body> <img src="<%=request.getContextPath() %>/images/common/error.jpg"> <br> <s:if test="exception.errorMsg != '' && exception.errorMsg != null"> <s:property value="exception.errorMsg"/> </s:if> <s:else> 操作失败!<s:property value="exception.message"/> </s:else> </body>
效果:
抽取BaseAction
我们在用Action的时候,未免都会存在一些功能的属性。例如:在listUI,我们要获取多个用户的时候,需要有selectedRow这么一个属性。在其他的子模块也应该要有这个样属性。所以我们可以抽取出来—>形成一个BaseAction。其他的Action只要继承着BaseAction就有相对应的属性了。public class BaseAction extends ActionSupport { public String[] selectedRow; public String[] getSelectedRow() { return selectedRow; } public void setSelectedRow(String[] selectedRow) { this.selectedRow = selectedRow; } }
制定返回类型StrutsResultSupport
在有特殊情况时;如果没有异常信息,但是有错误并且有错误信息等内容;此时也需要进行友好的错误处理的话,那么可以借助StrutsResultSupport 返回结果类型来实现特定处理。此种方式先需要继承StrutsResultSupport ,然后可以在子类中获取本次请求的相关信息,再根据相关信息进行结果处理:
import com.opensymphony.xwork2.ActionInvocation; import org.apache.struts2.ServletActionContext; import org.apache.struts2.dispatcher.StrutsResultSupport; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class SysResultAction extends StrutsResultSupport { @Override protected void doExecute(String arg0, ActionInvocation invocation) throws Exception { HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); BaseAction action = (BaseAction)invocation.getAction(); //do something System.out.println("进入了 SysResultAction ..."); } }
配置:
<!-- 配置全局结果及异常映射 --> <package name="base-default" extends="struts-default"> <!-- 返回结果类型 --> <result-types> <result-type name="error" class="zhongfucheng.action.SysResultAction"></result-type> </result-types> <!-- 全局返回结果 --> <global-results> <result name="error" type="error">/WEB-INF/jsp/error.jsp</result> <result name="sysError">/WEB-INF/jsp/error.jsp</result> <result name="input">/WEB-INF/jsp/error.jsp</result> </global-results> <!-- 全局异常映射 --> <global-exception-mappings> <exception-mapping result="sysError" exception="zhongfucheng.action.SysResultAction"></exception-mapping> <exception-mapping result="input" exception="java.lang.Exception"></exception-mapping> </global-exception-mappings> </package>
相关文章推荐
- 纳税服务系统【异常处理、抽取BaseAction】
- 纳税服务系统【抽取BaseService、条件查询】
- 纳税服务系统【抽取BaseService、条件查询】
- 跟着汤阳光同志做一个OA系统(八):抽取页面公共部分、提取BaseAction、ModelDriven支持、Service和DAO合并、BaseAction中Service声明、非常好的增删改查流程
- 纳税服务系统【投诉受理管理,显示投诉信息、处理回复、我要投诉】
- 纳税服务系统【投诉受理管理,显示投诉信息、处理回复、我要投诉】
- 系统异常捕获处理
- 数据仓库自动抽取:通过 SQL Server 企业管理器中的数据转换服务 (DTS) 设计器 创建 Analysis Services 处理任务
- 调用TUXEDOF服务抛出的TPETRAN(14)异常处理.
- 调用webservices服务一个异常处理:org.xml.sax.SAXException: SimpleDeserializer encountered a child element, which is NOT expected, in someth
- WCF技术剖析之十:调用WCF服务的客户端应该如何进行异常处理
- progressive类型的jpeg在嵌入系统中的异常处理
- J2EE系统异常的处理准则
- OpenRS—— 开放式遥感数据处理与服务平台 OpenRS-Cloude:基于MapReduce的并行遥感处理系统
- [导入]构建WCF面向服务的应用程序系列课程(4):异常与错误处理.zip(13.70 MB)
- Silverlight 3 中改进的对于获取WCF服务的异常信息处理
- 利用异常进行系统中通用的消息通知和事件处理
- 针对有效的错误处理设计异常管理系统
- 关于UDP消息服务抛出“远程主机强迫关闭了一个现有的连接”的异常说明及处理方法
- 批处理方式设置XP系统的服务程序