¸ÃÀ¹½ØÆ÷λÓÚdefaultStackµÚ¶þµÄλÖã¬Æ书ÄܱȽϼòµ¥£¬Ö÷Òª¾ÍÊÇΪÎÒÃÇ´«µÝµÄÇëÇó²ÎÊýÈ¡±ðÃûÓ³Éäµ½¶à¸öÃüÃûµÄ²ÎÊýÉÏ£¬ÕâÑù¿ÉÒÔÈöà¸öActionÒÔ²»Í¬µÄ²ÎÊýÃû¹²Ïíͬһ¸öÇëÇó²ÎÊý£¬ÌرðÊÇÔÚResultÀàÐÍΪchainµÄʱºò¡£ÏÂÃæ¾Ù¸öÀý×Ó½²Ò»ÏÂÈçºÎÅäÖÃÕâ¸öÀ¹½ØÆ÷£º
±í1
<action name="alias" class="cn.itcast.action.AliasAction"> <param name="aliases">#{ 'un' : 'username' }</param> <result name="success">/WEB-INF/page/message.jsp</result> </action> |
ÔÚÕâ¸öActionÖд«µÝÁËÒ»¸ö²ÎÊý£¬Ãû³ÆΪaliases£¬¸ÃÃû³ÆÊÇAliasInterceptorÀ¹½ØÆ÷ÖеÄĬÈÏÖµ£¬Èç¹û²»ÊÖ¶¯Ö¸¶¨£¬Ãû³Æ±ØÐëΪaliases¡£²ÎÊýµÄÖµµÄÐÎʽ±ØÐëÊÇÕâÑù£º
#{ 'key' :'value' }£¬ÆäʵÕâÊÇÒ»¸öOGNL±í´ïʽ£¬²»Öª´ó¼ÒÊÇ·ñ»¹¼ÇµÃ£¬OGNL¿ÉÒÔʹÓñí´ïʽ¶¯Ì¬¹¹ÔìÒ»¸öMap£¬ÆäÓï·¨¾ÍÊÇÕâÑùµÄ£¬Èç¹ûÓжà¸ö¼üÖµ¶ÔÒÔ¶ººÅ½øÐзָô£¬¾¹ýOGNL½âÎöºóÕâЩ²ÎÊý¾Í»áÔÚÒ»¸öMapÖС£
¶ÔÓÚÉÏÃæµÄÀý×Ó£¬ÎÒÃǸøÇëÇó²ÎÊýunÈ¡ÁËÒ»¸ö±ðÃûΪusername£¬ÕâÑùµ±ÎÒÃÇÔÚ·ÃÎʸÃActionµÄʱºò´«µÝÕâÑùµÄ¸ö²ÎÊý/alias.action?un=zhangsan£¬ÔÚAliasActionÖÐÓÃusernameÒ²ÄܶÔÆä½øÐнÓÊÕ£¬Èç¹ûAliasAction·µ»ØµÄResultÀàÐÍΪchainµÄ»°£¬ÄÇôÈç¹ûchainÖ¸ÏòµÄAction±ðÃûÅäÖÃΪ£º<param name="aliases">#{ 'un' : 'name'}</param>µÄ»°£¬ÔÚchainÖ¸ÏòµÄActionÖоͿÉÒÔÓÃnameÀ´¶Ôun½øÐнÓÊÕ¡£
ÏÂÃæ¿´Ò»ÏÂAliasInterceptorµÄÔ´Â룬ÕâÀïÁгöÆäintercept·½·¨£¬¿´ÆäÊÇÈçºÎʵÏָù¦Äܵģº
±í2
@Override public String intercept(ActionInvocation invocation) throws Exception {
ActionConfig config = invocation.getProxy().getConfig();//»ñÈ¡ActionÅäÖà ActionContext ac = invocation.getInvocationContext();//»ñÈ¡ActionContext Object action = invocation.getAction();//»ñÈ¡µ±Ç°Ö´ÐеÄAction
// get the action's parameters »ñÈ¡ActionµÄ²ÎÊý£¬×¢ÒâÕâÀïµÄ²ÎÊý²»ÊÇrequestÇëÇó²ÎÊý¶øÊÇ //ÔÚstruts2ÅäÖÃÎļþÖÐͨ¹ý<param>½ÚµãÅäÖõIJÎÊý final Map<String, String> parameters = config.getParams(); //aliasesKey¾ÍÊÇ<param name="aliases">½ÚµãµÄnameÊôÐÔÖµ if (parameters.containsKey(aliasesKey)) {//Èç¹ûÅäÖÃÁËaliases
String aliasExpression = parameters.get(aliasesKey);//»ñÈ¡ÎÒÃÇ´«µÝµÄ±í´ïʽ ValueStack stack = ac.getValueStack(); Object obj = stack.findValue(aliasExpression);//È¥ÖµÕ»ÖвéÕÒ±í´ïʽ´ú±íµÄÖµ
if (obj != null && obj instanceof Map) { //get secure stack ValueStack newStack = valueStackFactory.createva lueStack(stack); boolean clearableStack = newStack instanceof Clearableva lueStack; if (clearableStack) { //Ê¡ÂÔ... }
// override Map aliases = (Map) obj;//ǿתΪMap£¬ÒòΪ·µ»ØµÄ¾ÍÊÇLinkedHashMap¶ÔÏó£¬ÓÃLinkedHashMap²ÅÄܱ£Ö¤ÅäÖòÎÊýÓÐÐò for (Object o : aliases.entrySet()) {//µü´ú Map.Entry entry = (Map.Entry) o; String name = entry.getKey().toString();//ÔÃû£¬ÉÏÃæµÄÀý×ÓÖоÍÊÇ'un' String alias = (String) entry.getValue();//±ðÃû£¬ÉÏÃæµÄÀý×ÓÖоÍÊÇ'username' Object value = stack.findValue(name);//È¥ÖµÕ»ÖвéÕÒ£¬Ò»°ãÀ´ËµÊÇÕÒ²»µ½µÄ if (null == value) { // workaround »ñÈ¡requestÇëÇó²ÎÊýMap Map<String, Object> contextParameters = ActionContext.getContext().getParameters();
if (null != contextParameters) { value = contextParameters.get(name);//°ÑÇëÇó²ÎÊýÖÐÏàÓ¦µÄÖµ»ñÈ¡³öÀ´£¬ÉÏÃæµÄÀý×ÓÖÐÖµ¾ÍÊÇ'zhangsan' } } if (null != value) { try { newStack.setValue(alias, value);//°ÑÇëÇó²ÎÊýÖÐÏàÓ¦µÄÖµ»ñÈ¡À´ÒÔ±ðÃûΪkeyÔÙ¸³¸øÖµÕ» } catch (RuntimeException e) { if (devMode) { String developerNotification = LocalizedTextUtil.findText(ParametersInterceptor.class, "devmode.notification", ActionContext.getContext().getLocale(), "Developer Notification:\n{0}", new Object[]{ "Unexpected Exception caught setting '" + entry.getKey() + "' on '" + action.getClass() + ": " + e.getMessage() }); LOG.error(developerNotification); if (action instanceof ValidationAware) { ((ValidationAware) action).addActionMessage(developerNotification); } } } } } //Ê¡ÂÔ... } else { LOG.debug("invalid alias expression:" + aliasesKey); } }
return invocation.invoke(); } |
ÉÏÃæÊ¡ÂÔÁËһЩÎ޹ؽôÒªµÄ´úÂë¡£
ÉÏÃæµÄ´úÂë×¢ÊÍÓ¦¸Ã½âÊ͵ñȽÏÇå³þÁË£¬´ó¼Ò¿ÉÒÔ¿´µ½´ÓÇëÇó²ÎÊýÖлñÈ¡Öµºó£¬ÔÙÒÔ±ðÃûΪkeyÔÙ¸³¸øÖµÕ»£¬ÔÚÉÏÃæµÄÀý×ÓÖÐkey(±ðÃû)¾ÍÊÇusername£¬Í¨¹ýsetValue(key,value)¸øÖµ¼ú¸³ÖµµÄʱºòÊÇ´ÓÕ»¶¥¿ªÊ¼ÍùÏÂÑ°ÕÒÒ»¸öÓÐusernameÊôÐÔµÄÒ»¸ö¶ÔÏó£¬È»ºó°ÑÖµ¸³¸ø¸Ã¶ÔÏóÔÙ·µ»Ø£¬¶øÔÚÖµÕ»ÖÐÕ»¶¥¶ÔÏó¾ÍÊǵ±Ç°Ö´ÐеÄAction£¬ËùÒԾͰѸÃÖµ¾Í¸³¸øÁ˵±Ç°Ö´ÐеÄAction¡£Í¬ÑùµÄµÀÀí£¬Èç¹û·µ»ØµÄResultÀàÐÍΪchain£¬ÕâʱÕâ¸öÁ´ÖеÄActionÊǹ²Ïíͬһ¸örequest¶ÔÏóµÄ£¬Èç¹ûÔÚchainÁ´ÖеÄActionÒ²ÅäÖÃÁËÏàÓ¦µÄ±ðÃû²ÎÊý£¬Ò²¿ÉÒÔͨ¹ýÆä±ðÃû½ÓÊÕÇëÇó²ÎÊýÁË¡£