exceptionÀ¹½ØÆ÷¶ÔÓ¦µÄÀàΪcom.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor ¸ÃÀ¹½ØÆ÷ÊÇstruts2רÃÅÓÃÓÚ´¦ÀíÒì³£µÄ£¬Èç¹ûÎÒÃǽøÐÐÁËÒì³£ÅäÖ㬵±struts2µÄÖ´ÐгöÏÖÒì³£ºó¿ÉÒÔÌøתµ½Ö¸¶¨µÄResult£¬ÕâÒ²ÊÇstruts2µÄÒì³£´¦Àí»úÖÆ¡£
ÏÂÃ濴һϸÃÀ¹½ØÆ÷µÄÔ´Â룺
±í1
publicclass ExceptionMappingInterceptor extends AbstractInterceptor { //Ê¡ÂÔ... @Override public String intercept(ActionInvocation invocation) throws Exception { String result;
try { result = invocation.invoke();//Ö´ÐÐÏÂÒ»¸öÀ¹½ØÆ÷ } catch (Exception e) { if (isLogEnabled()) { handleLogging(e); } //»ñÈ¡Òì³£Ó³ÉäÅäÖà List<ExceptionMappingConfig> exceptionMappings = invocation.getProxy().getConfig().getExceptionMappings(); //´ÓÒì³£Ó³Éä»ñÈ¡result String mappedResult = this.findResultFromExceptions(exceptionMappings, e); if (mappedResult != null) { result = mappedResult;//Èç¹û»ñÈ¡µÄresult²»ÎªnullÔòÌæ»»ÔÀ´µÄresult publishException(invocation, new ExceptionHolder(e));//·¢²¼Òì³£ } else { throwe; } }
returnresult;//·µ»Øresult } //Ê¡ÂÔ... } |
ÏÞÓÚƪ·ù£¬ÕâÀïÖ»ÁгöÁËÆä×îÖØÒªµÄintercept·½·¨£¬µÈÓõ½ÆäËü·½·¨µÄʱºòÔÙÁгö¡£¿ÉÒÔ¿´µ½¸ÃÀ¹½ØÆ÷¼Ì³Ð×ÔAbstractInterceptor£¬AbstractInterceptorʵÏÖÁËInterceptor½Ó¿Ú£¬AbstractInterceptorºÜ¼òµ¥£¬¾ÍÊǶÔInterceptor½Ó¿ÚÖÐÉùÃ÷µÄinitÓëdestroy·½·¨½øÐÐÁË¿ÕʵÏÖ£¬ÕâÑù×öµÄºÃ´¦ÀíÊÇÈç¹ûÎÒÃÇ×Ô¼ºÐ´µÄÀ¹½ØÆ÷¼Ì³Ð×ÔAbstractInterceptor¾Í²»ÓÃȥʵÏÖinitÓëdestroy·½·¨ÁË£¬Èç¹ûÐèÒªÕâÁ½¸ö·½·¨Ö»Òª½øÐи²¸Ç¼´¿É£¬ÕâÑù±È½Ï·½±ã£¬½ö´Ë¶øÒÑ¡£
¸ÃÀ¹½ØÆ÷±È½ÏÌØÊ⣬֮ËùÒÔ˵ÆäÌØÊâÊÇÒòΪÆäÔÚdefaultStackÖÐËù´¦µÄλÖû¹ÓÐÆäintercept·½·¨ÖÐinvocation.invoke()Ö´Ðеĵط½¡£exceptionÀ¹½ØÆ÷´¦ÓÚdefaultStackÖеÚÒ»¸öλÖ㬱íÃæÉÏÊǵÚÒ»¸öλÖ㬵«´ó¼Ò¿ÉÒÔ¿´µ½result =invocation.invoke();Ç°ÃæûÓÐÈκÎÂß¼´úÂ룬¶øÊÇÖ±½ÓÖ´ÐÐÏÂÒ»¸öÀ¹½ØÆ÷£¬¼ÓÉÏÔ½ºóÖ´ÐеÄÀ¹½ØÆ÷Ô½ÏÈÖ´ÐÐÍê³É·µ»Ø£¬ÕâÑù¾Í½«µ¼ÖÂresult = invocation.invoke();ºóÃæµÄ´úÂëÊÇÔÚÆäËüËùÓÐÀ¹½ØÆ÷Ö´ÐÐÍê³Éºó²Å»áÖ´ÐС£struts2ÕâÑù×öµÄÔÒòÊÇÓɸÃÀ¹½ØÆ÷µÄ¹¦ÄÜËù¾ö¶¨µÄ¡£ÒòΪ¸ÃÀ¹½ØÆ÷µÄÄ¿µÄÊÇÒª¶ÔÒì³£½øÐд¦Àí£¬¶øÒì³£¿ÉÒÔ³öÏÖÔÚÿһ¸öÀ¹½ØÆ÷ÖУ¬ËùÒÔ¼¯Öд¦ÀíÒì³£µÄ´úÂëÒª·Åµ½×îºóÖ´ÐУ¬ËùÒÔÕâ¾Í¾ö¶¨Á˸ÃÀ¹½ØÆ÷Òª·ÅÖõ½defaultStackµÄµÚÒ»¸öλÖ㬲¢ÇÒÔÚintercept·½·¨ÖÐÖ±½Óµ÷ÓÃÏÂÒ»¸öÀ¹½ØÆ÷¡£ÔÚstruts2ÖпÉÒÔ¶¨Òå¶à¸öÀ¹½ØÆ÷Õ»£¬Æäʵ²»¹ÜÀíÊÇÄÄÒ»¸öÀ¹½ØÆ÷Õ»£¬exceptionÀ¹½ØÆ÷¶¼Ó¦¸ÃÊÇ´¦ÀíµÚÒ»¸öλÖá£
result = invocation.invoke();ÕâÀïËäȻֻÓÐÒ»¾ä£¬ÆäʵÊÇ°üº¬Á˸ÃÀ¹½ØÆ÷ºóÃæµÄËùÓÐÀ¹½ØÆ÷°üÀ¨ActionµÄÖ´ÐУ¬Èç¹ûÔÚÖ´ÐеĹý³ÌÖÐÅ׳öÒì³£Ôò»á½øÐе½catch¿éÖУ¬Ê×ÏÈ»ñÈ¡Òì³£Ó³ÉäÅäÖÃÐÅÏ¢£¬È»ºó´ÓÒì³£Ó³ÉäÅäÖÃÐÅÏ¢²éÕÒresult£¬Èç¹ûÕÒµ½ÁËÔò½«ÔÀ´µÄresult½øÐÐÌæ»»£¬ÕâÑù·µ»ØµÄresult¾Í»áÖ¸ÎÒÃÇÔÚÅäÖÃÎļþÖÐÅäÖõÄÒì³£Result£¬ÒÔ´ïµ½´¦ÀíÒì³£µÄÄ¿µÄ¡£
ÏÂÃæÊÇfindResultFromExceptions·½·¨µÄÔ´Â룺
±í2
protected String findResultFromExceptions(List<ExceptionMappingConfig> exceptionMappings, Throwable t) { String result = null; // Check for specific exception mappings. if (exceptionMappings != null) { int deepest = Integer.MAX_VALUE; for (Object exceptionMapping : exceptionMappings) { ExceptionMappingConfig exceptionMappingConfig = (ExceptionMappingConfig) exceptionMapping; int depth = getDepth(exceptionMappingConfig.getExceptionClassName(), t); if (depth >= 0 && depth < deepest) { deepest = depth; result = exceptionMappingConfig.getResult(); } } } return result; } |
getDepth·½·¨Ô´Â룺
±í3
publicint getDepth(String exceptionMapping, Throwable t) { return getDepth(exceptionMapping, t.getClass(), 0); }
privateint getDepth(String exceptionMapping, Class exceptionClass, int depth) { if (exceptionClass.getName().contains(exceptionMapping)) { //Èç¹ûÅ׳öµÄÒ쳣ƥÅäµ½ÁËÔò·´¼ÓÒ»¸öÉî¶ÈÖµ // Found it! return depth; } // If we've gone as far as we can go and haven't found it... if (exceptionClass.equals(Throwable.class)) {//Èç¹ûÒѾ¼ìË÷µ½ÁËThrowableÔò·µ»Ø-1 return -1; } return getDepth(exceptionMapping, exceptionClass.getSuperclass(), depth + 1); }
|
findResultFromExceptions·½·¨Öд«ÈëÁËÒì³£Ó³ÉäÅäÖÃÐÅÏ¢£¬Ò²¾ÍÊÇÔÚstruts2ÅäÖÃÎļþÖеÄ<exception-mapping result=""exception=""/>½Úµã£¬ÒòΪÎÒÃÇ¿ÉÄÜÅäÖöà¸ö<exception-mappingresult="" exception=""/>½Úµã£¬ËùÒÔ´«½øÀ´µÄÊÇÒ»¸öList£¬Èç¹û²»ÎªnullÔò¶ÔÆä½øÐеü´ú¡£int depth = getDepth(exceptionMappingConfig.getExceptionClassName(),t);Õâ¾ä´úÂëÓÃÓÚ»ñÈ¡Ò»¸öÉî¶ÈÖµ£¬Õâ¾ä´úÂë¼ÓÉÏÉÏÏÂÃæµÄifÓï¾äÅжϾö¶¨ÁËÄÄÒ»ÖÖÒì³£Ö¸ÏòÄÄÒ»¸öResult¡£
Ê×ÏÈÉùÃ÷ÁËÒ»¸öÃûΪdeepestÕûÐαäÁ¿£¬³õʼֵΪInteger.MAX_VALUE(ÕâÖ»ÊǸö³õʼֵ£¬¸ÃÖµ»áÔÚºóÃæµü´úµÄ¹ý³Ì±»ÐÞ¸Ä)£¬ÔÚµü´úµÄ¹ý³ÌÖУ¬µÚÒ»´Î´úÂëdepthΪ0£¬Èç¹ûÔÚÀ¹½ØÆ÷ÓëActionµÄÖ´Ðйý³ÌÖÐÅ׳öµÄÒ쳣ƥÅäµ½ÁËÎÒÃÇÅäÖõÄÒì³£Ôò»á·´Õâ¸öÉî¶ÈÖµ£¬Èç¹ûûÓÐÆ¥Åäµ½Ôò»á¼ÌÐøÆ¥Åä¸ÃÅ׳öµÄÒì³£ÀàµÄ¸¸À࣬²¢ÇÒÉî¶ÈÖµ¼Ó1£¬¼´Õâ¾ä´úÂ룺getDepth(exceptionMapping,exceptionClass.getSuperclass(), depth + 1);ÕâÀïʹÓÃÁ˵ݹ飬ÆäÍ˳öÌõ¼þÊÇÆ¥Åäµ½ÁËÏàÓ¦µÄÒì³££¬»òÕß¼ìË÷µ½ÁËThrowableÀ࣬ÒòΪËùÓÐÒì³£¶¼¼Ì³Ð×ÔThrowable£¬Èç¹û¼ìË÷µ½ÁËThrowableÀà¾Íû±ØÒªÔÙÍùÉϼìË÷ÁË£¬Ö±½Ó·µ»Ø-1(¼´Ã»ÓÐÆ¥Åäµ½ÅäÖõÄÒì³£)£¬»ñÈ¡µ½ÁËdepthÉî¶ÈÖµºóÖ´ÐÐifÌõ¼þÅжϣ¬Õâ¸öÅжϺÜÖØÒª£¬ÎÒÃÇÌØÒ⽫ËüÄùýÀ´£º
±í4
if (depth >= 0 && depth < deepest) { deepest = depth; result = exceptionMappingConfig.getResult(); } |
Èç¹ûdepth >= 0 && depth < deepestÔò»áÖ´ÐÐifÓï¾ä¿éÖеĴúÂ룬depth >= 0Ôò±íʾÕâ´Îµü´ú¹ý³ÌÖÐÆ¥Åäµ½ÁËÎÒÃÇÅäÖõÄÒì³££¬µÚÒ»´Îµü´údepth < deepestÊǿ϶¨³ÉÁ¢µÄ£¬ËùÒÔµÚÒ»´Îµü´úÈç¹ûÆ¥ÅäÉÏÁ˾ͻáÖ´ÐÐifÓï¾ä¿éÖеĴúÂ룬´úÂëÖн«deepestÐÞ¸ÄΪÁËÉÏÒ»´Î·µ»ØµÄdepthÖµ²¢¶Ôresult±äÁ¿Ê¹ÓÃÒì³£Ó³ÉäÖеÄresult½øÐи³Öµ¡£Èç¹ûûÓÐÆ¥Åäµ½ÎÒÃÇÅäÖõÄÒì³£Ôòresult²»»á±»¸³Öµ¡£ÔÚºóÃæµÄµü´ú¹ý³ÌÈç¹ûÓÖÆ¥ÅäÉÏÁ˽øÐÐifÅжϵÄʱºòÓ¦¸Ã×¢Ò⣬ÕâʱµÄdeepestÖµÊÇÉÏÒ»´ÎÆ¥Åä·µ»ØµÄÉî¶ÈÖµ£¬Èç¹ûÕâ´Î·µ»ØµÄÉî¶ÈÖµ±ÈÉÏÒ»´Î·µ»ØµÄÉî¶ÈֵС²Å»áÖ´ÐÐifÓï¾ä¿éÖеĴúÂ룬Éî¶ÈÖµÊÇͨ¹ýµÝ¹éµ÷Óõõ½ÁËÒ²¾ÍÊÇdepthÉî¶ÈֵԽСÔò¸ÃÒì³£Ô½¾ßÌ壬ÒòΪƥÅ丸Òì³£µÄʱºòÉî¶È×Ô¼Ó1£¬ËùÒÔµ±Å׳öµÄÒ쳣ƥÅäÉ϶à¸öÎÒÃÇÔÚÅäÖÃÎļþÖÐÅäÖõÄÒì³£Ó³ÉäµÄʱºòstruts2»áÑ¡Ôñ×î¾ßÌåµÄÒì³£Ó³Éä¡£¾Ù¸öÀý×Ó£¬ÏÂÃæÊÇÒì³£ÅäÖãº
±í5
<global-exception-mappings> <exception-mapping result="error1" exception="java.lang.Exception"/> <exception-mapping result="error2" exception="java.lang.IndexOutOfBoundsException"/> </global-exception-mappings>
|
ÕâÀïÎÒÃÇÅäÖÃÁËÁ½¸öÒì³£Ó³É䣬Èç¹ûÔÚÀ¹½ØÆ÷ÓëActionµÄÖ´Ðйý³Ì¹ý³ÌÖÐÅ׳öµÄÊÇIndexOutOfBoundsExceptionÔò¼´»áÆ¥ÅäµÚÒ»¸öÒì³£Ó³ÉäÒ²»áÆ¥ÅäµÚ¶þ¸öÒì³£Ó³É䣬µ«Êǵڶþ¸ö¸ü¾ßÌå¼´·µ»ØµÄÉî¶ÈÖµ¸üС£¬ËùÒÔÔÚÅ׳öIndexOutOfBoundsExceptionµÄʱºòstruts2»áÑ¡Ôñerror2Õâ¸öresult¡£
ÏÖÔڻص½¸ÃÀ¹½ØÆ÷µÄintercept·½·¨ÖС£Èç¹ûfindResultFromExceptions·½·¨Æ¥Åäµ½ÁËÎÒÃÇÅäÖõÄÒì³£Ôò»á·µ»ØÒì³£Ó³ÉäÖеÄresultÖµ£¬Èç¹ûûÓÐÆ¥ÅäÉÏÔò·µ»Ønull£¬Èç¹û²»ÎªnullµÄ»°£¬½«ÔÀ´µÄresultÖµÌ滻ΪÎÒÃÇÒì³£Ó³ÉäÖеÄresultÖµ²¢·¢²¼Òì³££¬Èç¹ûΪnullÔò·µ»ØÔÀ´µÄresult£¬ÔÚÒ³ÃæÉϾͻáÏÔʾ³öÒì³£Õ»ÐÅÏ¢¡£ÕâÑùÈç¹ûÆ¥ÅäÉÏÁ˳öÀ´ÁËÒì³£µ½ÁËǰ̨ÏÔʾµÄʱºò¾Í»áÏÔʾÎÒÃÇÖ¸¶¨µÄÒ³Ãæ¡£
ÏÂÃæ¿´Ò»ÏÂpublishException(invocation, new ExceptionHolder(e));·½·¨Ëù×öµÄ¹¤×÷£º
±í6
protectedvoid publishException(ActionInvocation invocation, ExceptionHolder exceptionHolder) { invocation.getStack().push(exceptionHolder); } |
¸Ã·½·¨ºÜ¼òµ¥£¬¾ÍÊǽ«Å׳öµÄÒì³£°ü×°½øÁËÒ»¸ö½ÐExceptionHolderÀàÐ͵ĶÔÏóÖУ¬È»ºóѹÈëÁËÖµÕ»µÄÕ»¶¥£¬ÕâÑùÎÒÃǾͿÉÒÔÊÇÊÓͼÖлñÈ¡Òì³£ÐÅÏ¢(Èç¹ûÐèÒªµÄ»°)¡£
×îºó½²Ò»ÏÂÆ¥ÅäÒì³£µÄϸ½Ú£¬¾ÍÊÇ£ºif (exceptionClass.getName().contains(exceptionMapping))Õâ¸öifÌõ¼þÅжϣ¬ÕâÑùstruts2ÅжÏÓÐÎÞÆ¥ÅäÉÏÓõÄÊÇcontains·½·¨£¬¼´ÎÒÃÇÔÚÅäÖÃÒì³£Ó³ÉäµÄʱºò²»ÐèÒª½«Òì³£ÀàÃûдȫҲ¿ÉÒÔ£¬±ÈÈ磺
±í7
<exception-mapping result="error2" exception="java.lang.IndexOutOfBoundsException"/> ÎÒÃÇÅäÖóÉ: <exception-mapping result="error2" exception="java.lang.IndexOutOf"/> |
Ò²¿ÉÒÔÆ¥ÅäÉÏIndexOutOfBoundsException£¬ÕâÀï¸öÈ˾õµÃ¸üºÃµÄ×ö·¨Ó¦¸ÃÊǽ«ËùÅ׳öµÄÒì³£ÀàµÄÀàÃûÓëÅäÖÃÒì³£Ó³ÉäÅжÏÊÇ·ñÏàµÈ£¬ÕâÑù²Å¸üÑϽ÷£¬¼´contains·½·¨¸ÄΪequals·½·¨¡£
µ½ÕâÀïexceptionÀ¹½ØÆ÷µÄ¹¤×÷ÔÀíÓ빦Äܾͽ²½â¾Í½áÊø......