StaticParametersInterceptorÀ¹½ØÆ÷¼ò½é
¸ÃÀ¹½ØÆ÷´¦ÓÚdefaultStackµÚÊ®ÈýµÄλÖã¬ÊÇÓÃÀ´°Ñͨ¹ý<param>±êÇ©ÅäÖÃÔÚstruts2ÅäÖÃÎļþÖÐΪActionÅäÖõľ²Ì¬²ÎÊýÌî×°µ½ActionÖÐÈç¹ûActionʵÏÖÁËParameterizable½Ó¿Ú£¬»¹»á°Ñ¾²Ì¬²ÎÊý·â×°³ÉÒ»¸öMapÖ±½Ó´«µÝ¸øAction£¬ÕâЩ¾²Ì¬²ÎÊý»¹»áÌí¼Óµ½request²ÎÊýMapÖгý·Ç¸ÃÀ¹½ØÆ÷µÄmergeÊôÐÔΪfalse£¬¸ÃÊôÐÔֵĬÈÏÊÇΪtrueµÄ¡£ÈçϾÍÊÇÒ»¶Î¾²Ì¬²ÎÊýÅäÖãº
±í1
<action name="staticparams" class="com.xtayfjpk.action.StaticParametersAction"> <result>/WEB-INF/page/message.jsp</result> <param name="username">zhangjin</param> <param name="password">xtayfjpk</param> </action> |
ÏÂÃæÊǸÃÀ¹½ØÆ÷µÄintercept·½·¨Ô´Â룺
±í2
@Override public String intercept(ActionInvocation invocation) throws Exception { ActionConfig config = invocation.getProxy().getConfig();//»ñÈ¡ActionÅäÖà Object action = invocation.getAction();//»ñÈ¡µ±Ç°Ö´ÐеÄAction
//»ñÈ¡µ±Ç°ActionµÄ¾²Ì¬²ÎÊýMap£¬×¢Òâ²»ÊÇrequestÇëÇó²ÎÊýMap final Map<String, String> parameters = config.getParams(); //Ê¡ÂÔ... // Èç¹ûActionʵÏÖÁËParameterizable½Ó¿Ú if (action instanceof Parameterizable) {
//µ÷ÓÃActionµÄsetParams·½·¨£¬Ö±½ÓÉèÖõ½ActionÖÐ ((Parameterizable) action).setParams(parameters); } //Èç¹ûActionÅäÖÃÓо²Ì¬²ÎÊý if (parameters != null) { ActionContext ac = ActionContext.getContext();//»ñÈ¡ActionContext¶ÔÏó
//»ñÈ¡ActionContext¶ÔÏóÄÚ²¿µÄcontext Map¶ÔÏ󣬼´OgnlContext¶ÔÏó Map<String, Object> contextMap = ac.getContextMap(); try { //Ê¡ÂÔ... final ValueStack stack = ac.getValueStack();//»ñÈ¡ValueStack //Ê¡ÂÔ... for (Map.Entry<String, String> entry : parameters.entrySet()) {//µü´ú¾²Ì¬²ÎÊýMap Object val = entry.getValue();//»ñÈ¡µ±Ç°²ÎÊýÖµ
//Èç¹ûparseΪtrueÔòÒª½«²ÎÊýÖµÒª×÷Ϊһ¸ö±í´ïʽ½øÐмÆË㣬parseĬÈÏΪfalse if (parse && val instanceof String) { //½«·µ»ØµÄÖµ½øÐÐÌæ»» val = TextParseUtil.translateVariables(val.toString(), stack); } try {//½«µ±Ç°²ÎÊýÉèÖõ½ÖµÕ»ÖÐ newStack.setValue(entry.getKey(), val); } catch (RuntimeException e) { //Ê¡ÂÔ... } } //Ê¡ÂÔ... if (merge)//ÊÇ·ñÐèÒªºÏ²¢£¬Ä¬ÈÏΪtrue addParametersToContext(ac, parameters);//½«¾²Ì¬²ÎÊýÌí¼Óµ½ActionContextÖÐ } finally { //Ê¡ÂÔ... } } return invocation.invoke();//µ÷ÓÃÏÂÒ»¸öÀ¹½ØÆ÷ } |
ÉÏÃæµÄÔ´´úÂëÖÐÊ¡ÂÔÁËһЩ·ÇÖØÒª´úÂ룬Èç¹ûÒª²é¿´ÍêÕûÔ´ÂëÇë²Î¿´struts2×Ô´øÔ´Âë¡£
ʵ¼ÊÉϸ÷½·¨µÄÂß¼»¹ÊDZȽϼòµ¥µÄ£¬¾ÍÊÇ°ÑActionÅäÖÃÖеľ²Ì¬²ÎÊýÌî×°µ½ActionÖжøÒÑ£¬Õâ¸öÊÇͨ¹ýnewStack.setValue(entry.getKey(), val);Õâ¾ä´úÂëʵÏֵģ¬ÒòΪActionÒ»°ãÊÇ´¦ÀíValueStackÕ»¶¥£¬Èç¹ûActionÖÐÓвÎÊý½ÓÊյĻ°¾Í°Ñ²ÎÊýÖµ¸³¸øÁËAction¡£
Èç¹û¸ÃÀ¹½ØÆ÷µÄmerge(ĬÈÏΪtrue)ÊôÐÔΪtrue£¬Ôò»¹»á°Ñ¾²Ì¬²ÎÊýÌí¼Óµ½ActionContextÖУ¬ÏÂÃæÊÇaddParametersToContext·½·¨Ô´Â룺
±í3
protectedvoid addParametersToContext(ActionContext ac, Map<String, ?> newParams) { Map<String, Object> previousParams = ac.getParameters();//»ñÈ¡ÇëÇó²ÎÊý
Map<String, Object> combinedParams; if ( overwrite ) {//ÊÇ·ñÐèÒª¶ÔÇëÇó²ÎÊý½øÐи²¸Ç if (previousParams != null) { //ÕâÀï·ÅµÄÊÇpreviousParamsÇëÇó²ÎÊý combinedParams = new TreeMap<String, Object>(previousParams); } else { combinedParams = new TreeMap<String, Object>(); } if ( newParams != null) { //Ôٷž²Ì¬²ÎÊý£¬Èç¹ûÓÐkeyÏàͬ£¬ÔòÇëÇó²ÎÊý¸²¸ÇÁ˾²Ì¬²ÎÊý£¬¾ÍÏ൱ÓÚûÓи²¸ÇÇëÇó²ÎÊý combinedParams.putAll(newParams); } } else { if (newParams != null) { combinedParams = new TreeMap<String, Object>(newParams);//ÏÈ´æ·Å¾²Ì¬²ÎÊý } else { combinedParams = new TreeMap<String, Object>(); } if ( previousParams != null) { //ÔÙ·ÅÇëÇó²ÎÊý£¬Èç¹ûÓÐkeyÏàͬ£¬Ôò¾²Ì¬²ÎÊý¸²¸ÇÁËÇëÇó²ÎÊý combinedParams.putAll(previousParams); } } //½«´¦Àí¹ýºóµÄ²ÎÊýÉèÖõ½ActionContextÖÐ ac.setParameters(combinedParams); } |
ͨ¹ýÉÏÃæµÄ·½·¨Íê³ÉÁ˽«¾²Ì¬²ÎÊýÌí¼Óµ½ActionContextÖУ¬µ½ÕâÀï¸ÃÀ¹½ØÆ÷Âß¼¾Í½²½âÍê±ÏÁË£¬×îºóµ÷ÓÃinvocation.invoke();µ÷ÓÃÏÂÒ»¸öÀ¹½ØÆ÷......
¸ÃÀ¹½ØÆ÷Öл¹ÓÐÒ»¸öÃûΪretrieveParametersµÄ·½·¨£¬Êǹ¦ÄÜÊÇ»ñÈ¡¾²Ì¬²ÎÊýµÄ£¬µ«ÊǸ÷½·¨È´²¢Ã»ÓÐÔÚ¸ÃÀàÖÐʹÓã¬ÕâÓ¦¸ÃÊǸÃÀ¹½ØÆ÷×÷ÕßÒÅÍüÁË¡£