本博文主要講解在項目中異常是怎麼處理的。通常咱們都不會直接把後臺異常信息返回給用戶,用戶是看不懂的。讓用戶看見一大串的錯誤代碼,這是不合理的。所以咱們須要對報錯進行處理。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>