1.javaä¸status1åstatus2çåºå«ï¼
2.struts2 java.lang.reflect.InvocationTargetException
javaä¸status1åstatus2çåºå«ï¼
ããStruts1åStruts2çåºå«å对æ¯:
ããAction ç±»:
ããStruts1è¦æ±Action类继æ¿ä¸ä¸ªæ½è±¡åºç±»ãStruts1çä¸ä¸ªæ®éé®é¢æ¯ä½¿ç¨æ½è±¡ç±»ç¼ç¨èä¸æ¯æ¥å£ã
ããStruts 2 Actionç±»å¯ä»¥å®ç°ä¸ä¸ªActionæ¥å£ï¼ä¹å¯å®ç°å ¶ä»æ¥å£ï¼ä½¿å¯éåå®å¶çæå¡æ为å¯è½ãStruts2æä¾ä¸ä¸ªActionSupportåºç±»å»å®ç° 常ç¨çæ¥å£ãActionæ¥å£ä¸æ¯å¿ é¡»çï¼ä»»ä½æexecuteæ è¯çPOJO对象é½å¯ä»¥ç¨ä½Struts2çAction对象ã
ãã线ç¨æ¨¡å¼:
ããStruts1 Actionæ¯åä¾æ¨¡å¼å¹¶ä¸å¿ é¡»æ¯çº¿ç¨å®å ¨çï¼å ä¸ºä» æActionçä¸ä¸ªå®ä¾æ¥å¤çææç请æ±ãåä¾çç¥éå¶äºStruts1 Actionè½ä½çäºï¼å¹¶ä¸è¦å¨å¼åæ¶ç¹å«å°å¿ãActionèµæºå¿ é¡»æ¯çº¿ç¨å®å ¨çæåæ¥çã
ããStruts2 Action对象为æ¯ä¸ä¸ªè¯·æ±äº§çä¸ä¸ªå®ä¾ï¼å æ¤æ²¡æ线ç¨å®å ¨é®é¢ãï¼å®é ä¸ï¼servlet容å¨ç»æ¯ä¸ªè¯·æ±äº§ç许å¤å¯ä¸¢å¼ç对象ï¼å¹¶ä¸ä¸ä¼å¯¼è´æ§è½ååå¾åæ¶é®é¢ï¼
ããServlet ä¾èµ:
ããStruts1 Action ä¾èµäºServlet API ,源码知己棋牌纯源码å 为å½ä¸ä¸ªAction被è°ç¨æ¶HttpServletRequest å HttpServletResponse è¢«ä¼ éç»executeæ¹æ³ã
ããStruts 2 Actionä¸ä¾èµäºå®¹å¨ï¼å 许Actionè±ç¦»å®¹å¨åç¬è¢«æµè¯ãå¦æéè¦ï¼Struts2 Actionä»ç¶å¯ä»¥è®¿é®åå§çrequeståresponseãä½æ¯ï¼å ¶ä»çå ç´ åå°æè æ¶é¤äºç´æ¥è®¿é®HttpServetRequest å HttpServletResponseçå¿ è¦æ§ã
ããå¯æµæ§:
ããæµè¯Struts1 Actionçä¸ä¸ªä¸»è¦é®é¢æ¯executeæ¹æ³æ´é²äºservlet APIï¼è¿ä½¿å¾æµè¯è¦ä¾èµäºå®¹å¨ï¼ãä¸ä¸ªç¬¬ä¸æ¹æ©å±ï¼ï¼Struts TestCaseï¼ï¼æä¾äºä¸å¥Struts1ç模æ对象ï¼æ¥è¿è¡æµè¯ï¼ã
ããStruts 2 Actionå¯ä»¥éè¿åå§åã设置å±æ§ãè°ç¨æ¹æ³æ¥æµè¯ï¼âä¾èµæ³¨å ¥âæ¯æä¹ä½¿æµè¯æ´å®¹æã
ããæè·è¾å ¥:
ããStruts1 使ç¨ActionForm对象æè·è¾å ¥ãææçActionFormå¿ é¡»ç»§æ¿ä¸ä¸ªåºç±»ãå ä¸ºå ¶ä»JavaBeanä¸è½ç¨ä½ActionFormï¼å¼åè ç»å¸¸å建å¤ä½çç±»æè·è¾å ¥ãå¨æBeanï¼DynaBeansï¼å¯ä»¥ä½ä¸ºåå»ºä¼ ç»ActionFormçéæ©ï¼ä½æ¯ï¼å¼åè å¯è½æ¯å¨éæ°æè¿°(å建)å·²ç»åå¨çJavaBeanï¼ä»ç¶ä¼å¯¼è´æåä½çjavabeanï¼ã
ãã Struts 2ç´æ¥ä½¿ç¨Actionå±æ§ä½ä¸ºè¾å ¥å±æ§ï¼æ¶é¤äºå¯¹ç¬¬äºä¸ªè¾å ¥å¯¹è±¡çéæ±ãè¾å ¥å±æ§å¯è½æ¯æèªå·±(å)å±æ§çrich对象类åãActionå±æ§è½å¤éè¿ web页é¢ä¸çtaglibs访é®ãStruts2ä¹æ¯æActionForm模å¼ãrich对象类åï¼å æ¬ä¸å¡å¯¹è±¡ï¼è½å¤ç¨ä½è¾å ¥/è¾åºå¯¹è±¡ãè¿ç§ ModelDriven ç¹æ§ç®åäºtaglib对POJOè¾å ¥å¯¹è±¡çå¼ç¨ã
ãã表达å¼è¯è¨ï¼
ããStruts1 æ´åäºJSTLï¼å æ¤ä½¿ç¨JSTL ELãè¿ç§ELæåºæ¬å¯¹è±¡å¾éåï¼ä½æ¯å¯¹éååç´¢å¼å±æ§çæ¯æå¾å¼±ã
ããStruts2å¯ä»¥ä½¿ç¨JSTLï¼ä½æ¯ä¹æ¯æä¸ä¸ªæ´å¼ºå¤§åçµæ´»ç表达å¼è¯è¨ï¼ï¼"Object Graph Notation Language" (OGNL).
ããç»å®å¼å°é¡µé¢ï¼viewï¼:
ããStruts 1使ç¨æ åJSPæºå¶æ对象ç»å®å°é¡µé¢ä¸æ¥è®¿é®ã
ããStruts 2 ä½¿ç¨ "ValueStack"ææ¯ï¼ä½¿taglibè½å¤è®¿é®å¼èä¸éè¦æä½ ç页é¢ï¼viewï¼å对象ç»å®èµ·æ¥ãValueStackçç¥å 许éè¿ä¸ç³»åå称ç¸åä½ç±»åä¸åçå±æ§éç¨é¡µé¢ï¼viewï¼ã
ããç±»å转æ¢ï¼
ããStruts 1 ActionForm å±æ§é常é½æ¯Stringç±»åãStruts1使ç¨Commons-Beanutilsè¿è¡ç±»å转æ¢ãæ¯ä¸ªç±»ä¸ä¸ªè½¬æ¢å¨ï¼å¯¹æ¯ä¸ä¸ªå®ä¾æ¥è¯´æ¯ä¸å¯é ç½®çã
ããStruts2 使ç¨OGNLè¿è¡ç±»å转æ¢ãæä¾åºæ¬å常ç¨å¯¹è±¡ç转æ¢å¨ã
ããæ ¡éªï¼
ããStruts 1æ¯æå¨ActionFormçvalidateæ¹æ³ä¸æå¨æ ¡éªï¼æè éè¿Commons Validatorçæ©å±æ¥æ ¡éªãåä¸ä¸ªç±»å¯ä»¥æä¸åçæ ¡éªå 容ï¼ä½ä¸è½æ ¡éªå对象ã
ããStruts2æ¯æéè¿validateæ¹æ³åXWorkæ ¡éªæ¡æ¶æ¥è¿è¡æ ¡éªãXWorkæ ¡éªæ¡æ¶ä½¿ç¨ä¸ºå±æ§ç±»ç±»åå®ä¹çæ ¡éªåå å®¹æ ¡éªï¼æ¥æ¯æchainæ ¡éªåå±æ§
ããActionæ§è¡çæ§å¶ï¼
ããStruts1æ¯ææ¯ä¸ä¸ªæ¨¡åæåç¬çRequest Processorsï¼çå½å¨æï¼ï¼ä½æ¯æ¨¡åä¸çææActionå¿ é¡»å ±äº«ç¸åççå½å¨æã
ããStruts2æ¯æéè¿æ¦æªå¨å æ ï¼Interceptor Stacksï¼ä¸ºæ¯ä¸ä¸ªActionå建ä¸åççå½å¨æãå æ è½å¤æ ¹æ®éè¦åä¸åçActionä¸èµ·ä½¿ç¨ã
ããStruts1æµç¨ï¼
ããï¼1ï¼ç¨æ·ç请æ±ä»¥HTTPæ¹å¼ä¼ è¾å°æå¡å¨ä¸ï¼æ¥å请æ±çæ¯ActionServlet.
ããï¼2ï¼ActionServletæ¥æ¶å°è¯·æ±åï¼ä¼æ¥æ¾Struts-config.xmlæ件æ¥ç¡®å®æå¡å¨ä¸æ¯å¦æç¨æ·è¯·æ±çæä½ï¼æ¤å¤ç¨æ·è¯·æ±æä½åºä¸ºç»éæä½ãå¦æ没æï¼åè¿åä¸ä¸ªç¨æ·è¯·æ±æ æçåºéä¿¡æ¯ã
ããï¼3ï¼å½ActionServlet请æ±æ¾å°ç¨æ·è¯·æ±çActionåï¼é¦å å°ç¨æ·è¾å ¥ç表ååæ°æå æä¸ä¸ªActionFrom对象ï¼è¿ä¸ªActionFromå¯¹è±¡å ¶å®ä¹å°±æ¯ä¸ä¸ªJavaBean,éé¢å å«ä¸¤ä¸ªå段ï¼åå«æ¯ç¨æ·ååå¯ç ãæ¥çActionServletåæ ¹æ®struts-config.xmlä¸çé 置信æ¯å³å®æ¯å¦è¦æ§è¡ActionFrom对象ä¸çValidateæ¹æ³ãè¥Validateæ¹æ³æ§è¡æéï¼åè¿åãå¦åï¼ç»§ç»ä¸ä¸æ¥ã
ããï¼4ï¼ç³»ç»çæä¸ä¸ªç¨æ·æ请æ±çActionçå®ä¾å¯¹è±¡ï¼å°åé¢çActionFromå¯¹è±¡ä¼ éç»å®ï¼è¿è¡å®çExecute()æ¹æ³ãè¿ä¸æ¥å ¶å®å°±æ¯ç¨æ·ç»éçæ§å¶å¨ï¼å¨æ§è¡exectue()æ¹æ³æ¶ï¼å¯ä»¥è°ç¨åå°æ¨¡åéªè¯ç»éååå¯ç æ¯å¦æ£ç¡®çä¿¡æ¯ã
ããï¼5ï¼execute()æ§è¡ç»æåä¼çæActionForwardç±»åç对象并å°ä¹è¿åç»ActionServlet
ããstruts1çå¤çå¨
ããprocessMultipart()è¦æ¯æ交æ¹å¼ä¸ºpostèä¸ç±»å为Multipartåè¿åéæ°å è£ çHttpServletRequest对象
ããprocessPath()è·åurlè·¯å¾ä»¥ä¾¿éæ©struts Actionç»ä»¶
ããprocessLocale()æç¨æ·æ交çLocaleä¿¡æ¯ä¿åå°sessionä¸
ããprocessContent()设置response.setContentTypeçå¼
ããprocessNoCache()设置æ¥å¤´Cache-Control Pragma Expiresçå¼
ããprocessPreprocess()ç´æ¥è¿åtrue åç±»å¯ä»¥è¦ç该æ¹æ³å®ç°å·±çæä½
ããprocessMapping()æ¥æ¾å¯¹åºçActionMapping ä¸åå¨åè¿åé误信æ¯
ããprocessRoles()å¤æ对åºçActionæ¯å¦é ç½®äºå®å ¨è§è² ç¨æ·æ¯å¦ææé
ããprocessActionForm()æ¥æ¾ActionMappingæ¯å¦é ç½®ActionForm æåæ¥æ¾è¯¥å®ä¾ä¸åå¨åå建
ããprocessPopulate()æ¥æ¾ActionMappingæ¯å¦é ç½®ActionForm æåè°ç¨resetæ¹æ³æ¸ 空åè£ è½½è¡¨åæ°æ®
ããprocessValidate()è°ç¨ActionFormçvalidateæ¹æ³è¿åActionErrors è¦æ¯åå¨ActionMessage对象åéªè¯å¤±è´¥
ããprocessForward()æ¥æ¾ActionMappingæ¯å¦é ç½®forward æåéç¨forwardç»ææµç¨
ããprocessInclude()æ¥æ¾ActionMappingæ¯å¦é ç½®include æåéç¨includeç»ææµç¨
ããprocessActionCreate()æ¥æ¾æ¯å¦æ对åºçActionå®ä¾ ä¸åå¨åå建
ããprocessActionPerform()è°ç¨Actionçexecuteæ¹æ³
ããprocessForwardConfig() å¤ç跳转
ããstruts2çæ§è¡æµç¨
ãã1ã客æ·ç«¯åèµ·ä¸ä¸ªè¯·æ±
ãã2ã主æ§å¨FilterDispatcheræ¦æªè¯·æ±.
ãã3ã主æ§å¨éè¿ActionMapperå¤æ请æ±æ¯å¦äº¤ç»action
ããå¤ç.å¦ææ¯,é£ä¹å¤æ请æ±çactionæ¯å¦åå¨,å¦æ ä¸åå¨,æ¥é误.å¦æä¸æ¯äº¤ç»actionå¤ç,é£ä¹å°
ãã请æ±è½¬åç»å¯¹åºçwebç»ä»¶.
ãã4ã主æ§å¨å建ActionProxy对象,è¿ä¸ªå¯¹è±¡å ¨æè´è´£ actionçæ§è¡.
ãã5ãActionProxyéè¿ConfigurationManager对象è·å¾å½ å访é®çactionçç¸å ³é 置信æ¯.
ãã6ãActionProxyæ ¹æ®è·å¾çactionçé 置信æ¯,å建
ããActionInvocation对象.
ãã7ãActionInvocation对象è´è´£æ§è¡actionåé¢çæ¦æªå¨
ãã以åaction对象,并ä¸å¤çactionæ§è¡åçç»æ.
ãã8ãactionåé¢çæ¦æªå¨è¢«æ§è¡,æåæ§è¡action.
ãã9ãActionInvocationå¾å°actionæ§è¡åçé»è¾è§å¾,æ ¹ æ®é»è¾è§å¾æ¾å°ç©çè§å¾,ç¶å转å请æ±.
struts2 java.lang.reflect.InvocationTargetException
nvocationTargetException æ¯ä¸ç§å è£ ç±è°ç¨æ¹æ³ææé æ¹æ³ææåºå¼å¸¸çåæ¥å¼å¸¸ãè¿ä¸ªå¼å¸¸å¹¶ä¸æ¯Eclipseæ件å¼åç¹æçï¼èæ¯æ åJDKä¸çï¼å®å®ä¹å¨ java.lang.reflectå ä¸ãå¨è¿è¡Javaå¼åçæ¶åå¾å°ä¼æ¥è§¦å°è¿ä¸ªå¼å¸¸ï¼ä¸è¿å¨è¿è¡Eclipseæ件å¼åä¸åä¸åï¼å¾å¤APIé½å£°ææåºæ¤ç±»å¼å¸¸ï¼å æ¤å¿ 须对æ¤å¼å¸¸è¿è¡å¤çã
ä¾å¦ï¼æ们å¼åä¸ä¸ªæ¹æ³ç¨æ¥ç»ä¸å¤çå¼å¸¸ï¼
private static void handleException(Exception e)
{
MessageDialog.openError(Activator.getDefault().getWorkbench()
.getDisplay().getActiveShell(), "error", e.getMessage());
e.printStackTrace();
}
æ们åç°å½ä¼ éæ¥çåæ°e为InvocationTargetException çæ¶åå¼¹åºç对è¯æ¡ä¸çæ¶æ¯æ¯ç©ºçï¼æ¥çInvocationTargetException çæºç å¾ç¥InvocationTargetException 并没æè¦çgetMessageæ¹æ³ï¼æ以æ¶æ¯å½ç¶æ¯ç©ºçäºãæ们éè¦è°ç¨InvocationTargetException çgetTargetExceptionæ¹æ³å¾å°è¦è¢«å è£ çå¼å¸¸ï¼è¿ä¸ªå¼å¸¸ææ¯çæ£æ们éè¦çå¼å¸¸ãä¿®æ¹ä»£ç å¦ä¸æ示ï¼
private static void handleException(Exception e)
{
String msg = null;
if (e instanceof InvocationTargetException)
{
Throwable targetEx = ((InvocationTargetException) e)
.getTargetException();
if (targetEx != null)
{
msg = targetEx.getMessage();
}
} else
{
msg = e.getMessage();
}
MessageDialog.openError(Activator.getDefault().getWorkbench()
.getDisplay().getActiveShell(), "error", msg);
e.printStackTrace();
}