CheckboxInterceptorÀ¹½ØÆ÷¼ò½é
¸ÃÀ¹½ØÆ÷´¦ÓÚdefaultStackµÚʮһµÄλÖ㬿´Õâ¸öÀ¹½ØÆ÷µÄÃû³ÆÓ¦¸Ã¾ÍÊǶÔcheckbox½øÐд¦ÀíµÄ¡£ÒªÀí½âÕâ¸öÀ¹½ØÆ÷µÄ×÷ÓÃÓÐÒ»µã±ØÐëÖªµÀ£º
µ±Ò»¸öform±íµ¥ÖдæÔÚÒ»¸öcheckboxµÄʱºò£¬¼ÙÉèÕâ¸öcheckboxµÄnameÊôÐÔֵΪmarried£¬µ±ÎÒÃÇÑ¡ÉÏÕâ¸öcheckboxµÄʱºòÇëÇó²ÎÊýÃüÁîÀïÓÐÒ»¸ömarried=true²ÎÊý£¬¶øµ±ÎÒÃÇûÓÐÑ¡ÉÏÕâ¸öcheckboxµÄʱºò£¬ÇëÇó²ÎÊýÀïÃæ¸ù±¾¾ÍûÓÐÕâ¸ömarried²ÎÊý£¬¾ÍÏ൱ÓÚform±íµ¥Öиù±¾¾ÍûÓÐÕâ¸öcheckbox£¬¶ø´ó¶àÊýÇé¿öÏÂÎÒÃÇÏëÒªµÄ½á¹ûÊÇ£¬µ±Ã»ÓÐÑ¡ÉÏÕâ¸öcheckboxµÄʱºòÇëÇó²ÎÊý¼¯ºÏÀïÓÐÕâ¸ömarried²ÎÊý£¬Ö»²»¹ýËüµÄֵΪ"false"¡£
ÎÊÌâÒý³öÀ´ÁË£¬¶ø¸ÃÀ¹½ØÆ÷¾Í¿ÉÒÔÓÃÀ´½â¾öÕâ¸öÎÊÌâ¡£ÏÂÃæÊǸÃÀ¹½ØÆ÷µÄintercept·½·¨Ô´Â룺
±í1
public String intercept(ActionInvocation ai) throws Exception { Map parameters = ai.getInvocationContext().getParameters();//»ñÈ¡ÇëÇó²ÎÊýMap Map<String, String[]> newParams = new HashMap<String, String[]>();//´´½¨Ò»¸öвÎÊýMap Set<Map.Entry> entries = parameters.entrySet(); //µü´úÇëÇó²ÎÊýMap for (Iterator<Map.Entry> iterator = entries.iterator(); iterator.hasNext();) { Map.Entry entry = iterator.next(); //»ñÈ¡µ±Ç°µü´úµÄÇëÇó²ÎÊýµÄkey£¬¼´form±íµ¥×ֶεÄnameÊôÐÔÖµ String key = (String)entry.getKey(); //ÅжÏkeyÊÇ·ñÒÔ__checkbox_¿ª¹Ø if (key.startsWith("__checkbox_")) { //»ñÈ¡µ±Ç°key __checkbox_ºóÃ沿·Ö×Ö·û´® String name = key.substring("__checkbox_".length()); //»ñÈ¡µ±Ç°entryµÄÖµ Object values = entry.getValue(); iterator.remove();//´ÓÇëÇó²ÎÊýMapÖÐÒƳýµ±Ç°entry if (values != null && values instanceof String[] && ((String[])values).length > 1) { //Èç¹û½øÈë¸ÃifÓï¾ä£¬Ôò˵Ã÷ͬһ¸öcheckboxÉèÖÃÁËÁ½´Î£¬ÔòÖ±½Ó½øÈëÏÂÒ»¸öentry LOG.debug("Bypassing automatic checkbox detection due to multiple checkboxes of the same name: #1", name); continue; }
// ÅжÏÃûΪ name±äÁ¿Öµ µÄcheckboxÊÇ·ñ±»Ñ¡ÖУ¬Ã»ÓÐÑ¡ÖÐÔò²»»áÓÐÏàÓ¦µÄ²ÎÊý if (!parameters.containsKey(name)) { // ûÓÐÑ¡ÖÐÔòÏòвÎÊýMapÖÐÌí¼ÓÒ»¸ökeyΪ name±äÁ¿Öµ µÄ³¤¶ÈΪ1µÄ×Ö·û´®Êý×é newParams.put(name, new String[]{uncheckedValue}); } } } //°ÑеIJÎÊýMap¼ÓÈëµ½ÇëÇó²ÎÊýMapÖÐ parameters.putAll(newParams); //µ÷ÓÃÏÂÒ»¸öÀ¹½ØÆ÷ return ai.invoke(); } |
ΪÁ˸üºÃµÄÀí½â¸ÃÀ¹½ØÆ÷µÄ¹¦ÄÜ£¬ÏÂÃæÁоÙÒ»¸ö±íµ¥£º
±í2
<form action="xtayfjpk.do" method="post"> <input type="checkbox" name="married" value="3"/>ÒÑ»é<br/> <input type="hidden" name="__checkbox_married"/><br/> <input type="submit" value="Ìá½»"/> </form>
|
µ±½øÈëµ½¸ÃÀ¹½ØÆ÷Ö®ºó£¬Åжϵ±Ç°µü´úµÄnameÊôÐÔÖµÊÇ·ñÒÔ__checkbox_¿ªÍ·£¬Èç¹û²»ÊÇÒÔ__checkbox_¿ª¹ØÔòûÓÐÈκβÙ×÷£¬Èç¹ûÒÔ__checkbox_¿ªÍ·Ôò°Ñ__checkbox_ºóÃæµÄ²¿·Ö½ØÈ¡³öÀ´£¬ÕâÀï¼´µÃµ½married£¬È»ºó»ñÈ¡_checkbox_married²ÎÊýÖµ£¬ÔÙ½«¸Ã²ÎÊý´ÓÇëÇó²ÎÊýMapÖÐÒƳý(ÒòΪÕâ¸ö²ÎÊýÖ»ÊÇÒ»¸ö±ê¼Ç¶øÒÑ)£¬È»ºó»ñÈ¡__checkbox_marriedµÄÖµ£¬Õý³£Çé¿öÓ¦¸ÃÊÇΪnullµÄ£¬µ«Èç¹ûÓÐÖµÔò˵Ã÷ÏàͬÒâÒåµÄcheckbox±»ÉèÖÃÁËÁ½´ÎÕâÑù¾Í»á´òÓ¡Ò»¸ödebugÐÅÏ¢²»ÔÙÖ´ÐÐÏÂÃæµÄ²Ù×÷¡£
Õý³£Çé¿öΪnullÔòÈ¥ÅжÏmarried checkboxÓÐÎÞ±»Ñ¡ÖУ¬Ñ¡ÖÐÁËmarried²ÎÊý¾Í»áÔÚrequest²ÎÊýMapÖУ¬Èç¹ûûÓÐÑ¡ÖÐÔò²»»á£¬µ«Í¨¹ýnewParams.put(name, new String[]{uncheckedValue});Õâ¾ä´úÂë¸ømarried¸³ÖµÁËÒ»¸ö³¤¶ÈΪ1µÄ×Ö·û´®Êý×éÆäֵΪuncheckedValue£¬¸ÃÖµÊÇ¿ÉÒÔÉèÖõģ¬Ä¬ÈÏֵΪ"false"£¬×îºó°ÑnewParamsÖеÄÊý¾ÝÈ«²¿Ìí¼Óµ½requestÇëÇó²ÎÊýMapÖУ¬Íê³ÉÂß¼£¬ÔÙµ÷ÓÃÏÂÒ»¸öÀ¹½ØÆ÷¡£
²»ÖªµÀ´ó¼ÒÓÐûÓз¢ÏÖ»ñÈ¡µÄÇëÇó²ÎÊýÖµ·µ»ØµÄÊÇString[]£¬¶ø²»ÊÇÒ»¸ö×Ö·û´®£¬¾ÍÄÃcheckbox¾Ù¸öÀý×Ó£¬Èç¹ûÓöà¸öcheckboxÈÃÓû§Ñ¡°®ºÃ£¬ÕâÑù¾Í¿ÉÄܳöÏÖ¶à¸ö°®ºÃ£¬ËùÒÔÒªÓÃÒ»¸öString[]È¥½ÓÊÕ£¬ÎªÁËͳһ²ÎÊý½ÓÊÕ£¬¼´ÊÇÒ»¸öÖµµÄ·µ»ØµÄÒ²ÊÇÒ»¸öString[]£¬Ö»²»¹ýÊý×鳤¶ÈΪ1¡£
¼ÈȻһ¸ö²ÎÊýÃû¿ÉÒÔ½ÓÊÕ¶à¸ö²ÎÊýÖµ£¬ÄÇΪʲôÉÏÃæÀý×Óµ±__checkbox_marriedÓÐÖµµÄʱºòÈ´²»ÔÙÖ´ÐÐÏÂÃæµÄ´úÂëÁËÄØ£¿ÕâÊÇÒòΪ__checkbox_marriedÕâ¸ö²ÎÊýÖ»ÊÇÓÃÀ´±êʶµ±married checkboxûÓÐÑ¡ÖеÄʱºò¸ÃÀ¹½ØÆ÷Òª²»Òª¶ÔÆä½øÐд¦Àí£¬¶øÕý³£Çé¿öÏÂÊDz»Ó¦¸ÃÓÐÖµµÄ¡£ÒªÊ¹¸ÃÀ¹½ØÆ÷ÓÐЧÔò±ØÐëÔÚform±íµ¥ÖÐÌí¼ÓÒ»¸öÃûΪ"__checkbox_"+Òª´¦ÀíµÄcheckboxµÄnameÊôÐÔÖµµÄ×ֶΡ£
ÕâÀïÄØÔÙ½²Ò»Ï¹ØÓÚrequest¶ÔÏóÈ¡²ÎÊýÖµ·½·¨µÄÒ»µãϸ½Ú£º
µ±µ÷ÓÃrequest.getParameter(key)·µ»ØµÄÊÇÒ»¸ö×Ö·û´®£¬µ÷ÓÃrequest.getParameterMap.get(key)·µ»ØµÄÊÇÒ»¸ö×Ö·û´®Êý×飬һ¿ªÊ¼¾õµÃÓеãÃԻ󣬾õµÃ·µ»ØÖµÓ¦¸ÃÊÇÏàͬµÄ£¬ÕâÀïÈ´³öÏÖÁ˲»Ò»ÑùµÄ½á¹û(keyÏàͬ)£¬ºóÀ´È¥·ÁËTomcatµÄÔ´Âë²ÅŪÃ÷°×¡£
request.getParameter(key)£¬Êµ¼Êµ÷ÓõÄÊÇorg.apache.tomcat.util.http.ParametersÀàÖеÄgetParameter(Stringname)·½·¨£º
±í3
public String getParameter(String name) { handleQueryParameters(); ArrayList values = (ArrayList)this.paramHashValues.get(name); if (values != null) { if (values.size() == 0) return "";
return ((String)values.get(0));//·µ»ØindexΪ0µÄÔªËØ } return null; } |
request.getParameterMap.get(key)£¬Êµ¼Êµ÷ÓõÄÊÇorg.apache.tomcat.util.http.ParametersÀàÖеÄgetParameterMap·½·¨£º
±í4
public Map getParameterMap() { if (parameterMap.isLocked()) return parameterMap; Enumeration enumeration = getParameterNames(); while (enumeration.hasMoreElements()) { String name = enumeration.nextElement().toString(); String[] values = getParameterValues(name);//»ñÈ¡ÖµÊý×é parameterMap.put(name, values);//·ÅÈëÁËparameterMapÖÐ } parameterMap.setLocked(true); return parameterMap; }
public String[] getParameterValues(String name) {//¸Ã·½·¨»ñÈ¡ÖµÊý×é handleQueryParameters(); ArrayList values = (ArrayList)this.paramHashValues.get(name); if (values == null) return null;
//½«values ArrayListת³ÉÁËString[]·µ»Ø return ((String[])values.toArray(new String[values.size()])); } |
¿´µ½ÉÏÃæµÄÔ´Âë´ó¼ÒÓ¦¸ÃÖªµÀΪʲôΪ³öÏÖ½á¹û²»Ò»ÖµÄÔÒòÁË¡£µ½´Ë¸ÃÀ¹½ØÆ÷¾Í½²½âÍê±ÏÁË£¬×¼±¸ÏÂÒ»¸öÀ¹½ØÆ÷À²......