日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Struts 声明式异常处理和个性化异常处理(转)

發布時間:2025/5/22 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Struts 声明式异常处理和个性化异常处理(转) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先我們的國際化資源文件中有

?

  • user.not.found?=?用戶不能找到,用戶名稱=[{0}] ??
  • user.password.error?=?user.password.error??
  • ?

    第一種 聲明式異常處理

    為國際化配置文件中的每個key,設計一個異常。

    用戶登錄驗證,用戶名錯誤拋出UserNotFoundException,密碼錯誤拋出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對應國際化配置文件中key,type是對應的異常處理類

    ?

    代碼 <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?錯誤時候?跳轉的頁面,優先級高于?action中的input?-->??
    ????????
    <!--?為每一個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類

    代碼 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)?{???
    ???????
    //?沒有配置,struts不管了,往web容器里拋,看在web.xml文件中是否配置,???
    ???????
    //?如果仍沒有配置,則把異常顯示到頁面上???
    ???????????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異常默認處理類???
    ???????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默認的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?{???
    ????
    //?沒有配置的話,返回actionForward就是<action>中的input???
    ????????forward?=?mapping.getInputForward();???
    ????}???
    ??
    ????
    //?Figure?out?the?error???
    ????if?(ex?instanceof?ModuleException)?{???
    ????????error?
    =?((ModuleException)?ex).getActionMessage();???
    ????????property?
    =?((ModuleException)?ex).getProperty();???
    ????}?
    else?{???
    ????
    //?我們寫的程序拋出的異常都是這種情況???
    ????
    //?從國際化文件中,根據key取???
    ????
    //?ae.getKey()?--?拿到<exception>中key值?user.not.found???
    ????????error?=?new?ActionMessage(ae.getKey(),?ex.getMessage());???
    ????????
    //?集合ActionMessages中的key,和國際化資源文件中的key一樣???
    ????????property?=?error.getKey();???
    ????}???
    ??
    ????
    this.logException(ex);???
    ??
    ????
    //?Store?the?exception???
    ????request.setAttribute(Globals.EXCEPTION_KEY,?ex);???
    ????
    //?放到ActionMessages集合中?getScope()默認request???
    ????this.storeException(request,?property,?error,?forward,?ae.getScope());???
    ??
    ????
    return?forward;?

    ?


    第二種 個性化異常處理

    ?

    代碼 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值隨便配一個?具體的是在程序中控制-->??
    ????
    <!--?type異常類?handler異常處理類?都是由我們來寫和控制?-->??
    ????
    <exception?key="error.exception"?type="demo.struts.ErrorCodeException"?handler="demo.struts.ErrorCodeExceptionHandler"/>??
    </global-exceptions>

    ?

    ?

    ?

    代碼 public?class?ErrorCodeException?extends?RuntimeException?{???
    ??
    ????
    //?錯誤碼就是國際化資源文件的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?{???
    ????????????
    //?原來的代碼?error?=?new?ActionMessage(ae.getKey(),?ex.getMessage());???
    ????????????
    //?ae.getKey()?拿出的key是配置文件中<exception>寫的,因為所有異常都用同一個key???
    ????????????
    //?無法找到國際化資源文件中的key,好在我們在拋異常時,把key寫進異常的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;???
    ??
    ????}??

    ?

    ?


    如果我們不想寫國際化配置文件,在程序中之際頁面提示內容

    ?

    <!--?用默認的handler也能能執行?-->??
    <exception?key="error.exception"?type="demo.struts.AppException"?handler="demo.struts.AppExceptionHandler"/>??

    ?

    國際化資源文件:

  • #?只有一個占位符 ??
  • error.exception={0} 代碼 public?void?login(String?username,?String?password)?{???
    ????
    if?(!"admin".equals(username))?{???
    ???????????????????????
    //?直接寫內容???
    ????????throw?new?AppException("用戶不能找到【"?+?username?+?"");???
    ????}???
    ????
    if?(!"admin".equals(password))?{???
    ????????
    throw?new?ErrorCodeException("密碼錯誤");???
    ????}???
    }??

    ?

  • ?

    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()?沒用???
    ????????????
    //?appe.getMessage()?("用戶不能找到【"?+?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;???
    ??
    ????}??

    ?

    ?

    ?

    轉載于:https://www.cnblogs.com/Fskjb/archive/2010/03/27/1698578.html

    總結

    以上是生活随笔為你收集整理的Struts 声明式异常处理和个性化异常处理(转)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。