Struts 声明式异常处理和个性化异常处理(转)
生活随笔
收集整理的這篇文章主要介紹了
Struts 声明式异常处理和个性化异常处理(转)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
首先我們的國(guó)際化資源文件中有
?
?
第一種 聲明式異常處理
為國(guó)際化配置文件中的每個(gè)key,設(shè)計(jì)一個(gè)異常。
用戶(hù)登錄驗(yàn)證,用戶(hù)名錯(cuò)誤拋出UserNotFoundException,密碼錯(cuò)誤拋出PasswordErrorException
?
?
代碼 public?void?login(String?username,?String?password)?{???????????if?(!"admin".equals())?{???
????????????throw?new?UserNotFoundException();???
????????}???
????????if?(!"admin".equals(password))?{???
????????????throw?new?PasswordErrorException();???
????????}???
????}?
?
struts-config.xml文件中,<exception/>的key對(duì)應(yīng)國(guó)際化配置文件中key,type是對(duì)應(yīng)的異常處理類(lèi)
?
代碼 <global-exceptions>??????<!--?為所有Action使用的,全局異常?-->??
????????<exception?key="user.not.found"?type="demo.struts.UserNotFoundException"/>??
????????<exception?key="user.password.error"?type="demo.struts.PasswordErrorException"/>??
????</global-exceptions>??
???????????
????<action-mappings>??
????????<action?path="/login"??
????????????????type="demo.struts.LoginAction"??
????????????????name="loginForm"??
????????????????scope="request"??
????????????????validate="false"??
????????????????input="/login.jsp"??
????????>???????
????????<!--?path?錯(cuò)誤時(shí)候?跳轉(zhuǎn)的頁(yè)面,優(yōu)先級(jí)高于?action中的input?-->??
????????<!--?為每一個(gè)action配置????
????????????<exception?key="user.not.found"?type="demo.struts.UserNotFoundException"?path="/login_error.jsp"/>??
????????????<exception?key="user.password.error"?type="demo.struts.PasswordErrorException"?path="/login_error.jsp"/>??
????????-->??
????????????<forward?name="success"?path="/login_success.jsp"/>??
????????????<forward?name="error"?path="/login.jsp"/>??
????????</action>??
???????????
????????<action?path="/changelang"??
????????????????type="demo.struts.ChangeLanguageAction"??
????????>??
????????????<forward?name="index"?path="/index.jsp"/>??
????????</action>??
??
????</action-mappings>??
?
?
Struts的RequestProcessor類(lèi)
代碼 protected?ActionForward?processException(HttpServletRequest?request,???????????????????????????????????????????????HttpServletResponse?response,???
????????????????????????????????????????????Exception?exception,???
????????????????????????????????????????????ActionForm?form,???
????????????????????????????????????????????ActionMapping?mapping)???
???????throws?IOException,?ServletException?{???
??
???????//?是否在配置了Exception???
???????ExceptionConfig?config?=?mapping.findException(exception.getClass());???
???????//?找到的config就是下面這條信息???
???????//?<exception?key="user.not.found"?type="demo.struts.UserNotFoundException"?path="/login_error.jsp"/>???
???????if?(config?==?null)?{???
???????//?沒(méi)有配置,struts不管了,往web容器里拋,看在web.xml文件中是否配置,???
???????//?如果仍沒(méi)有配置,則把異常顯示到頁(yè)面上???
???????????log.warn(getInternal().getMessage("unhandledException",???
?????????????????????????????????????????????exception.getClass()));???
???????????if?(exception?instanceof?IOException)?{???
???????????????throw?(IOException)?exception;???
???????????}?else?if?(exception?instanceof?ServletException)?{???
???????????????throw?(ServletException)?exception;???
???????????}?else?{???
???????????????throw?new?ServletException(exception);???
???????????}???
???????}???
??
???????//?struts異常默認(rèn)處理類(lèi)???
???????try?{???
???????????ExceptionHandler?handler?=?(ExceptionHandler)???
???????????RequestUtils.applicationInstance(config.getHandler());???
???????????return?(handler.execute(exception,?config,?mapping,?form,???
???????????????????????????????????request,?response));???
???????}?catch?(Exception?e)?{???
???????????throw?new?ServletException(e);???
???????}???
???}??
?
?
struts默認(rèn)的exceptionHandler
代碼 public?ActionForward?execute(???????Exception?ex,???
????ExceptionConfig?ae,???
????ActionMapping?mapping,???
????ActionForm?formInstance,???
????HttpServletRequest?request,???
????HttpServletResponse?response)???
????throws?ServletException?{???
??
????ActionForward?forward?=?null;???
????ActionMessage?error?=?null;???
????String?property?=?null;???
??
????//?在<exception>中配置了path,返回actionForward就是path中的配置???
????if?(ae.getPath()?!=?null)?{???
????????forward?=?new?ActionForward(ae.getPath());???
????}?else?{???
????//?沒(méi)有配置的話(huà),返回actionForward就是<action>中的input???
????????forward?=?mapping.getInputForward();???
????}???
??
????//?Figure?out?the?error???
????if?(ex?instanceof?ModuleException)?{???
????????error?=?((ModuleException)?ex).getActionMessage();???
????????property?=?((ModuleException)?ex).getProperty();???
????}?else?{???
????//?我們寫(xiě)的程序拋出的異常都是這種情況???
????//?從國(guó)際化文件中,根據(jù)key取???
????//?ae.getKey()?--?拿到<exception>中key值?user.not.found???
????????error?=?new?ActionMessage(ae.getKey(),?ex.getMessage());???
????????//?集合ActionMessages中的key,和國(guó)際化資源文件中的key一樣???
????????property?=?error.getKey();???
????}???
??
????this.logException(ex);???
??
????//?Store?the?exception???
????request.setAttribute(Globals.EXCEPTION_KEY,?ex);???
????//?放到ActionMessages集合中?getScope()默認(rèn)request???
????this.storeException(request,?property,?error,?forward,?ae.getScope());???
??
????return?forward;?
?
第二種 個(gè)性化異常處理
?
代碼 public?void?login(String?username,?String?password)?{???????????if?(!"admin".equals(username))?{???
????????????????????????//user.not.found資源文件中的key,?username?占位符???
????????????throw?new?ErrorCodeException("user.not.found",?username);???
????????}???
????????if?(!"admin".equals(password))?{???
????????????throw?new?ErrorCodeException("user.password.error");???
????????}??
?
?
代碼 <global-exceptions>??????<!--?key值隨便配一個(gè)?具體的是在程序中控制-->??
????<!--?type異常類(lèi)?handler異常處理類(lèi)?都是由我們來(lái)寫(xiě)和控制?-->??
????<exception?key="error.exception"?type="demo.struts.ErrorCodeException"?handler="demo.struts.ErrorCodeExceptionHandler"/>??
</global-exceptions>
?
?
?
代碼 public?class?ErrorCodeException?extends?RuntimeException?{?????
????//?錯(cuò)誤碼就是國(guó)際化資源文件的key???
????private?String?errorCode;???
??
????//?占位符???
????private?Object[]?args;???
??
????public?ErrorCodeException(String?errorCode)?{???
????????this(errorCode,?null);???
????}???
??
????public?ErrorCodeException(String?errorCode,?Object?arg)?{???
????????this(errorCode,?new?Object[]{arg});???
????}???
???????
????public?ErrorCodeException(String?errorCode,?Object[]?args){???
????????this.errorCode?=?errorCode;???
????????this.args?=?args;???
????}???
??
????//?只提供get方法???
????public?String?getErrorCode()?{???
????????return?errorCode;???
????}???
??
????public?Object[]?getArgs()?{???
????????return?args;???
????}??
?
?
?
?
代碼 public?class?ErrorCodeExceptionHandler?extends?ExceptionHandler?{?????
????public?ActionForward?execute(Exception?ex,?ExceptionConfig?ae,???
????????????ActionMapping?mapping,?ActionForm?formInstance,???
????????????HttpServletRequest?request,?HttpServletResponse?response)???
????????????throws?ServletException?{???
??
????????//?不是ErrorCodeException,我們不處理???
????????if?(!(ex?instanceof?ErrorCodeException))?{???
????????????return?super.execute(ex,?ae,?mapping,?formInstance,?request,?response);???
????????}???
???????????
????????ActionForward?forward?=?null;???
????????ActionMessage?error?=?null;???
????????String?property?=?null;???
??
????????//?Build?the?forward?from?the?exception?mapping?if?it?exists???
????????//?or?from?the?form?input???
????????if?(ae.getPath()?!=?null)?{???
????????????forward?=?new?ActionForward(ae.getPath());???
????????}?else?{???
????????????forward?=?mapping.getInputForward();???
????????}???
??
????????//?Figure?out?the?error???
????????if?(ex?instanceof?ModuleException)?{???
????????????error?=?((ModuleException)?ex).getActionMessage();???
????????????property?=?((ModuleException)?ex).getProperty();???
????????}?else?{???
????????????//?原來(lái)的代碼?error?=?new?ActionMessage(ae.getKey(),?ex.getMessage());???
????????????//?ae.getKey()?拿出的key是配置文件中<exception>寫(xiě)的,因?yàn)樗挟惓6加猛粋€(gè)key???
????????????//?無(wú)法找到國(guó)際化資源文件中的key,好在我們?cè)趻伄惓r(shí),把key寫(xiě)進(jìn)異常的errorCode中???
???????????????
????????????ErrorCodeException?ece?=?(ErrorCodeException)?ex;???
????????????//?拿到errorCode???
????????????String?errorCode?=?ece.getErrorCode();???
????????????//?拿到占位符???
????????????Object[]?args?=?ece.getArgs();???
???????????????
????????????error?=?new?ActionMessage(errorCode,?args);???
????????????property?=?error.getKey();???
????????}???
??
????????this.logException(ex);???
??
????????//?Store?the?exception???
????????request.setAttribute(Globals.EXCEPTION_KEY,?ex);???
????????this.storeException(request,?property,?error,?forward,?ae.getScope());???
??
????????return?forward;???
??
????}??
?
?
如果我們不想寫(xiě)國(guó)際化配置文件,在程序中之際頁(yè)面提示內(nèi)容
?
<!--?用默認(rèn)的handler也能能執(zhí)行?-->??<exception?key="error.exception"?type="demo.struts.AppException"?handler="demo.struts.AppExceptionHandler"/>??
?
國(guó)際化資源文件:
????if?(!"admin".equals(username))?{???
???????????????????????//?直接寫(xiě)內(nèi)容???
????????throw?new?AppException("用戶(hù)不能找到【"?+?username?+?"】");???
????}???
????if?(!"admin".equals(password))?{???
????????throw?new?ErrorCodeException("密碼錯(cuò)誤");???
????}???
}??
?
?
public?class?AppException?extends?RuntimeException?{???????public?AppException(String?msg){???
????????super(msg);???
????}???
}?? 代碼 public?class?AppExceptionHandler?extends?ExceptionHandler?{???
??
????public?ActionForward?execute(Exception?ex,?ExceptionConfig?ae,???
????????????ActionMapping?mapping,?ActionForm?formInstance,???
????????????HttpServletRequest?request,?HttpServletResponse?response)???
????????????throws?ServletException?{???
??
????????if(!(ex?instanceof?AppException)){???
????????????return?super.execute(ex,?ae,?mapping,?formInstance,?request,?response);???
????????}???
???????????
????????ActionForward?forward?=?null;???
????????ActionMessage?error?=?null;???
????????String?property?=?null;???
??
????????//?Build?the?forward?from?the?exception?mapping?if?it?exists???
????????//?or?from?the?form?input???
????????if?(ae.getPath()?!=?null)?{???
????????????forward?=?new?ActionForward(ae.getPath());???
????????}?else?{???
????????????forward?=?mapping.getInputForward();???
????????}???
??
????????//?Figure?out?the?error???
????????if?(ex?instanceof?ModuleException)?{???
????????????error?=?((ModuleException)?ex).getActionMessage();???
????????????property?=?((ModuleException)?ex).getProperty();???
????????}?else?{???
???????????????
????????????AppException?appe?=?(AppException)?ex;???
????????????//?ae.getKey()?沒(méi)用???
????????????//?appe.getMessage()?("用戶(hù)不能找到【"?+?username?+?"】???
????????????error?=?new?ActionMessage(ae.getKey(),?appe.getMessage());???
????????????property?=?error.getKey();???
????????}???
??
????????this.logException(ex);???
??
????????//?Store?the?exception???
????????request.setAttribute(Globals.EXCEPTION_KEY,?ex);???
????????this.storeException(request,?property,?error,?forward,?ae.getScope());???
??
????????return?forward;???
??
????}??
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/Fskjb/archive/2010/03/27/1698578.html
總結(jié)
以上是生活随笔為你收集整理的Struts 声明式异常处理和个性化异常处理(转)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 旅行商问题(Traveling Sale
- 下一篇: 2011考研英语词汇最佳记忆法 考量你的