本博文主要讲解在项目中异常是怎么处理的。通常咱们都不会直接把后台异常信息返回给用户,用户是看不懂的。让用户看见一大串的错误代码,这是不合理的。所以咱们须要对报错进行处理。java
咱们在开发的时候是使用层次来进行开发的。所以有三个层次:数据库
① Action层可能出现解析请求参数、返回结果有问题;apache
② Service 层则可能出现请求中要作的业务操做出现问题;出现了问题要根据实际状况判断是否会影响本次操做结果,action中要根据异常信息进行判断而后肯定是否操做成功;markdown
③ dao层也可能出如今操做数据库时出现错误;而此种错误通常都是致命的会影响操做结果。app
所以;在3个层次中至少要有两种类型的异常信息来标识。框架
异常类的定义应该放在core核心模块的exception包下的。jsp
/**** * 这是咱们自定义的总系统异常类 * * */ 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的异常类 * */ public class ActionException extends SysException { public ActionException() { super("请求操做失败了!"); } public ActionException(String message) { super(message); } }
/** * Created by ozc on 2017/5/26. */ public class ServiceException extends SysException { public ServiceException() { super("操做业务失败了!"); } public ServiceException(String message) { super(message); } }
咱们使用的是Struts2框架,想要报错的信息不直接给用户看见。就在Struts总配置文件中配置对应的映射。ide
<!-- 配置全局结果及异常映射 --> <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就好了。this
Serive层抛出异常:spa
@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了。仍是能够将它捕得到到
<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>
咱们在用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 ,而后能够在子类中获取本次请求的相关信息,再根据相关信息进行结果处理:
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>