纳税服务系统【异常处理、抽取BaseAction】
前言
本博文主要讲解在项目中异常是怎么处理的。一般我们都不会直接把后台异常信息返回给用户,用户是看不懂的。让用户看见一大串的错误代码,这是不合理的。因此我们需要对报错进行处理。
我们在开发的时候是使用层次来进行开发的。因此有三个层次:
① 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 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 到目前为止,我们已经写了三个模块的开发了.我们已经抽取过了BaseAction.BaseDao,我们这次来看看我们的Service接口. UserService /** * ...
- 纳税服务系统【信息发布管理、Ueditor、异步信息交互】
需求分析 我们现在来到了纳税服务系统的信息发布管理模块,首先我们跟着原型图来进行需求分析把: 一些普通的CRUD,值得一做的就是状态之间的切换了.停用和发布切换. 值得注意的是:在信息内容中,它可以带 ...
- 纳税服务系统【用户模块之使用POI导入excel、导出excel】
前言 再次回到我们的用户模块上,我们发现还有两个功能没有完成: 对于将网页中的数据导入或导出到excel文件中,我们是完全没有学习过的.但是呢,在Java中操作excel是相对常用的,因此也有组件供我 ...
- 纳税服务系统【统计图Fusionchart】
需求 我们在投诉模块中还有一个功能没有实现: 统计:根据年度将相应年度的每个月的投诉数进行统计,并以图表的形式展示在页面中:在页面中可以选择查看当前年度及其前4年的投诉数.在页面中可以选择不同的年度, ...
- 纳税服务系统【自动受理,Quartz任务调度】
需求 回到我们的需求: 自动投诉受理:在每个月月底最后一天对本月之前的投诉进行自动处理:将投诉信息的状态改为 已失效.在后台管理中不能对该类型投诉进行回复. 这个需求需求我们要怎么弄呢????要在每个 ...
- 基于Spring Boot、Spring Cloud、Docker的微服务系统架构实践
由于最近公司业务需要,需要搭建基于Spring Cloud的微服务系统.遍访各大搜索引擎,发现国内资料少之又少,也难怪,国内Dubbo正统治着天下.但是,一个技术总有它的瓶颈,Dubbo也有它捉襟见肘 ...
- IT服务系统组成
软件+硬件+数据 + 运维人员 = IT服务系统 车 司机 乘客 修车 = 车模式 效率 系统 用户 业务 运维 = 信息化 效率 如果司机不会开车,没有人会修车就不会有车轮上的世界 同样没有人会运维 ...
- 01——Solr学习之全文检索服务系统的基础认识
一.为什么要用Solr,Solr是个什么东西? 1.1.Solr是个开源的搜索服务器 1.2.我们用Solr主要实现搜索功能,一般的网站首页都会有一个大大的搜索框,用来搜索此网站上的商品啊什么的,如下 ...
- SpringCloud(9)使用Spring Cloud OAuth2保护微服务系统
一.简介 OAth2是一个标准的授权协议. 在认证与授权的过程中,主要包含以下3种角色. 服务提供方 Authorization Server. 资源持有者 Resource Server. 客户端 ...
随机推荐
- 关于Thread类的简单使用
线程:线程也被称为轻量级进程,进程和线程都提供一个执行环境,但创建一个新的线程比创建一个新的进程资源要少得多 线程存在进程里,也就是说一个进程至少包括一个线程 线程共享进程的资源,包括内存和打开的文件 ...
- RoboCup仿真3D TC笔记(2014年合肥中国公开赛 仿真3D比赛环境搭建)
所谓“TC“,就是Technology Committee(技术委员),讲的好像很厉害,实则就一“网管”. TC的技术含量其实不高,但是涉及的东西很多很杂,网上零零散散的都有,在这里我想总的整理一下, ...
- c#中的interface abstract与virtual介绍
abstract 与virtual : 方法重写时都使用 override 关键字,interface中的方法和abstract方法都要求实现 interface用来声明接口1.只提供一些方法规约, ...
- 常用px,pt,em换算及区别
pt (point,磅):是一个物理长度单位,指的是72分之一英寸. px (pixel,像素):是一个虚拟长度单位,是计算机系统的数字化图像长度单位,如果px要换算成物理长度,需要指定精度DPI(D ...
- hdu 6096---String(AC自动机)
题目链接 Problem Description Bob has a dictionary with N words in it.Now there is a list of words in whi ...
- SSH:分页实现
StudentAction: public class StudentAction extends ActionSupport { // 初始化下拉列表 @Resource private Stude ...
- Day-1: Python准备知识
python简介 不同于c语言这种贴近硬件的言语,Python是用来编写应用程序的高级编程语言.Python是一款开源软件,所以它有非常完善的代码库,宝库内置的基本库和众多开发者提供的第三方库.这就允 ...
- Spring4整合quartz2.2.3,quartz动态任务
Spring4整合quartz2.2.3,quartz动态任务 >>>>>>>>>>>>>>>>> ...
- MySQL(一)之MySQL简介与安装
大家可能都在用MySQL,其实我也是在用MySQL的,但是你知道吗?大部分人都是在windows中使用,这里将介绍一下在windows中的安装分为安装包安装与MSI包安装,以及在linux中的在线安装 ...
- 小而美的 React Form 组件
背景 之间在一篇介绍过 Table 组件< React 实现一个漂亮的 Table > 的文章中讲到过,在企业级后台产品中,用的最多且复杂的组件主要包括 Table.Form.Chart, ...