ActionMappingParametersInteceptorÀ¹½ØÆ÷¼ò½é
ActionMappingParametersInteceptorÀ¹½ØÆ÷´¦ÓÚdefaultStackµÚÊ®ËĵÄλÖ㬸ÃÀ¹½ØÆ÷¼Ì³Ð×ÔParametersInterceptor£¬Ò²ÊÇÓÃÓڰѲÎÊýÉèÖõ½ValueStackÖУ¬Ö»²»¹ý¸ÃÀ¹½ØÆ÷µÄ²ÎÊýÊÇÀ´Ô´ÓÚActionMappingÖУ¬¶ø²»ÊÇÀ´×ÔÇëÇó²ÎÊý¡£
ÍùValueStackÖÐÉèÖÃÖµµÄÂß¼ÓëParametersInterceptorһģһÑù£¬ÕýÊÇÕâ¸öÔÒò²Å°ÑParametersInterceptorÀ¹½ØÆ÷·ÅÔÚÁ˸ÃÀ¹½ØÆ÷֮ǰ½²½â¡£´ó¼Ò¿ÉÒÔ·¢ÏÖ¸ÃÀ¹½ØÆ÷Ö»ÊǸ²¸ÇÁËParametersInterceptorµÄÁ½¸ö·½·¨£º
±í1
@Override protected Map<String, Object> retrieveParameters(ActionContext ac) { //»ñÈ¡AcitonMapping¶ÔÏó ActionMapping mapping = (ActionMapping) ac.get(ServletActionContext.ACTION_MAPPING); if (mapping != null) { return mapping.getParams();//»ñÈ¡AcitonMapping²ÎÊý } else { return Collections.emptyMap(); } }
@Override//½«²ÎÊýÉèÖõ½ActionContextµÄparameters MapÖÐ protectedvoid addParametersToContext(ActionContext ac, Map newParams) { //»ñÈ¡ÇëÇó²ÎÊý Map previousParams = ac.getParameters(); Map combinedParams = null; if (previousParams != null) { combinedParams = new TreeMap(previousParams); } else { combinedParams = new TreeMap(); } //newParamsΪActionMappingÖеľ¹ýÁ˺Ϸ¨ÐÔ¼ì²éºóµÄºÏ·¨²ÎÊý combinedParams.putAll(newParams); //½«ÇëÇó²ÎÊýÓëºÏ·¨²ÎÊýÒ»ÆðÉèÖõ½ActionContextµÄparameters MapÖÐ ac.setParameters(combinedParams); } |
ÒòΪÏÖÔÚÖ´ÐеÄÊÇActionMappingParametersInteceptorÀ¹½ØÆ÷ËùÒÔÔÚParametersInterceptorµÄdoIntercept·½·¨Öе÷ÓõÄretrieveParameters·½·¨ÊÇActionMappingParametersInteceptorÀ¹½ØÆ÷ÖеÄretrieveParameters£¬ÕâÊÇjava¶à̬µÄ±íÏÖ£¬ËùÒÔ¸ÃÀ¹½ØÆ÷ͨ¹ý¸²¸Ç¸¸ÀàµÄretrieveParameters·½·¨´ïµ½Á˸ıä²ÎÊýÀ´Ô´µÄÄ¿µÄ¡£
ÔÚParametersInterceptorÀ¹½ØÆ÷µÄsetParameters·½·¨ÖÐ×îºóÒ»¾äµ÷ÓÃÁËaddParametersToContext·½·¨£¬¶øÔÚActionMappingParametersInteceptor×ÓÀàÖи²¸ÇÁËaddParametersToContext·½·¨£¬ËùÒÔÖ´ÐеÄÒ²ÊÇActionMappingParametersInteceptorÀ¹½ØÆ÷¶¨ÒåµÄaddParametersToContext·½·¨£¬½«½«ÇëÇó²ÎÊýÓëºÏ·¨²ÎÊýÒ»ÆðÉèÖõ½ActionContextµÄparameters MapÖС£
µ«ÓÐÒ»µãÐèÒª´ó¼Ò×¢Ò⣬¾ÍÊÇActionMappingµÄ²ÎÊýÓÀԶΪnull£¬ËùÒÔÖ´ÐиÃÀ¹½ØÆ÷ʱΪValueStackÉèÖÃÖµµÄ²Ù×÷¸ù±¾¾Í²»»á·¢Éú£¬ÏÂÃæͨ¹ýÔ´´úÂë½øÐÐÖ¤Ã÷¡£ÔÚÖ¤Ã÷֮ǰActionConfig£¬ActionMapping£¬ActionMapperÖ®¼äµÄ¹ØϵÐèÒªÃ÷°×£¬ActionConfig¶ÔÏó±£´æµÄÊÇActionµÄÅäÖÃÐÅÏ¢£¬ActionMapping¶ÔÏó±£´æµÄÊÇActionµÄÓ³ÉäÐÅÏ¢£¬ActionMapperÊǸù¾ÝÄã·ÃÎʵÄURLÈ¥ActionConfigÖнøÐÐÆ¥ÅäÔÙÓ³Éä³ÉActionMapping¶ÔÏó¡£ÀýÈ磬һ¸öActionµÄÅäÖÃÖпÉÒÔÓжà¸ö·½·¨£¬¿ÉÒÔÓжà¸öResult£¬µ«ÊÇÔÚÖ´ÐеÄʱºòÖ»ÄÜÖ´ÐÐActionÖеÄÒ»¸ö·½·¨£¬·µ»ØµÄʱºòÖ»ÄÜÓÐÒ»¸öResult¶øActionMapperµÄ¹¦ÄܾÍÊǸù¾ÝURLÓëAcitonConfigÓ³Éä³ÉActionMapping¶ÔÏó¡£
StrutsPrepareAndExecuteFilterµÄdoFilter·½·¨£º
±í2
publicvoid doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { //Ê¡ÂÔ... //µ÷ÓÃprepare¶ÔÏóµÄfindActionMapping ActionMapping mapping = prepare.findActionMapping(request, response, true); //Ê¡ÂÔ... } |
ÏÂÃæÊÇfindActionMapping·½·¨£º
±í3
public ActionMapping findActionMapping(HttpServletRequest request, HttpServletResponse response, boolean forceLookup) { ActionMapping mapping = (ActionMapping) request.getAttribute(STRUTS_ACTION_MAPPING_KEY); if (mapping == null || forceLookup) { try { //È¥ÈÝÆ÷ÖÐÕÒActionMapper¶ÔÏ󣬲¢µ÷ÓÃActionMapper¶ÔÏóµÄgetMapping·½·¨·µ»ØActionMapping¶ÔÏó mapping = dispatcher.getContainer().getInstance(ActionMapper.class).getMapping(request, dispatcher.getConfigurationManager()); if (mapping != null) { request.setAttribute(STRUTS_ACTION_MAPPING_KEY, mapping); } } catch (Exception ex) { dispatcher.sendError(request, response, servletContext, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex); } } return mapping; } |
ActionMapperÊÇÒ»¸ö½Ó¿Ú£¬ÔÚstruts2ÖÐÆäĬÈÏʵÏÖÀàÊÇorg.apache.struts2.dispatcher.mapper.DefaultActionMapper£¬ÏÂÃæÊǸÃÀàµÄgetMapping·½·¨£º
±í4
public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager) { ActionMapping mapping = new ActionMapping();//н¨Ò»¸öActionMapping¶ÔÏó String uri = getUri(request);//»ñÈ¡URI
int indexOfSemicolon = uri.indexOf(";"); uri = (indexOfSemicolon > -1) ? uri.substring(0, indexOfSemicolon) : uri;//´¦Àí URI
uri = dropExtension(uri, mapping);//È¥µôURIºó׺ if (uri == null) { return null; } //½âÎöActionµÄnamespaceÓëname parseNameAndNamespace(uri, mapping, configManager); //´¦ÀíÌØÊâ²ÎÊý handleSpecialParameters(request, mapping);
if (mapping.getName() == null) { return null; } //½âÎöActionName£¬ÒòΪstruts2¿ÉÒÔÖ§³Ö¶¯Ì¬·½·¨µ÷Óã¬È磺xxxAction!login parseActionName(mapping);
return mapping; } |
Ò»¿ªÊ¼ÒÔΪ»áÔÚhandleSpecialParametersÉèÖÃActionMapping²ÎÊý£¬µ«½øÈ¥¿´ÁËÒÔºó·¢ÏÖҲûÓÐΪActionMappingÉèÖòÎÊýµÄ²Ù×÷£¬ËùÒÔ¶ÔÓÚÓÃDefaultActionMapper×÷ΪActionMapperµÄµÄʵÏÖÀàµÄ»°£¬ActionMappingÖеIJÎÊýÊÇΪnullµÄ¡£
µ½´Ë¸ÃÀ¹½ØÆ÷¾Í½²µ½ÕâÀïÁË£¬×¼±¸ÏÂÒ»¸öÀ¹½ØÆ÷......