国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

springmvc對(duì)同名參數(shù)處理-我們到底能走多遠(yuǎn)系列(44)

2019-11-14 15:11:06
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

sPRingmvc對(duì)同名參數(shù)處理

扯淡:

中斷發(fā)博客幾個(gè)月,其實(shí)蠻不爽的,可能最近太忙太勞累了些,很多總結(jié)也沒(méi)時(shí)間寫,今天剛好遇到個(gè)小問(wèn)題,就閱讀下源碼找找樂(lè)。因?yàn)榭紤]到網(wǎng)上大多是提供解決問(wèn)題的方案,沒(méi)有實(shí)際去看spring源碼流程,所以就發(fā)個(gè)博文記錄下,萬(wàn)一以后有同學(xué)搜到我的文章能深入看些東西吧。

 

問(wèn)題描述:

前端有多個(gè)相同name的input:

<input type="text"  min="1" max="31" name="xName" value="1" /><input type="text"  min="1" max="31" name="xName" value="2" /><input type="text"  min="1" max="31" name="xName" value="3" />
 
提交時(shí)流程原理:
一般容器在HTTP協(xié)議的Request傳遞到ServletContainer中后,Container就會(huì)為該次請(qǐng)求生成一個(gè)HTTPServletRequest對(duì)象,在HTTPServletRequest對(duì)象中,參數(shù)和值,放入到了Map中。
tomcat源碼中放入map時(shí)的代碼:
   /**     * Put name and value pair in map.  When name already exist, add value     * to array of values.     *     * @param map The map to populate     * @param name The parameter name     * @param value The parameter value     */    private static void putMapEntry( Map map, String name, String value) {        String[] newValues = null;        String[] oldValues = (String[]) map.get(name);        if (oldValues == null) {            newValues = new String[1];            newValues[0] = value;        } else {            newValues = new String[oldValues.length + 1];            System.arraycopy(oldValues, 0, newValues, 0, oldValues.length);            newValues[oldValues.length] = value;        }        map.put(name, newValues);    }

 

可見(jiàn)同名的name,都會(huì)被放入數(shù)組的value中,而在servlet中,獲取value的兩個(gè)方法:

request.getParameter request.getParameterValues

第一個(gè)獲取的是數(shù)組的第一個(gè)值,第二個(gè)則獲得整個(gè)數(shù)組。

springmvc框架在遇到這種同名參數(shù)提交,而參數(shù)中有逗號(hào)時(shí),會(huì)出現(xiàn)問(wèn)題:
比如我提交的值是 xname=123 和 xname=45,67
那么在進(jìn)入action拿到參數(shù)時(shí)會(huì)變成string[] xname = ["123","45","67"]
就會(huì)影響業(yè)務(wù)邏輯正確執(zhí)行。
 

解決方案:

就是在提交后臺(tái)前,將參數(shù)進(jìn)行轉(zhuǎn)碼,后臺(tái)再進(jìn)行解碼就可以了。至于采用哪一種轉(zhuǎn)碼解碼方式,其實(shí)沒(méi)有什么要求,只需要注意采用方案會(huì)將逗號(hào)轉(zhuǎn)碼即可,知道這個(gè)關(guān)鍵點(diǎn)就好了。
 
實(shí)踐方案:
前端代碼:
$("input[name='ssidName']").each(function(){                        $(this).val(encodeURIComponent($(this).val()));                    })

 

后端java代碼:

URLDecoder.decode(ssidNames [i], "UTF-8" )

 

靈活點(diǎn)的想法是:采用可逆轉(zhuǎn)的轉(zhuǎn)碼解碼也可,比如base64。有興趣的可以嘗試。
 

源碼閱讀:

 
雖然知道spring有這個(gè)自動(dòng)組裝的功能,那么我就找到這段代碼:

關(guān)于springmvc原理分析這文章不錯(cuò):http://www.survivalescaperooms.com/heavenyes/p/3905844.html

 

我就在它后面再更進(jìn)一步分析同名參數(shù)是如何處理的。

完成request中的參數(shù)和方法參數(shù)上數(shù)據(jù)的綁定:

private Object[] resolveHandlerArguments(Method handlerMethod, Object handler,            NativeWebRequest webRequest, ExtendedModelMap implicitModel) throws Exception {     // 1.獲取方法參數(shù)類型的數(shù)組        Class[] paramTypes = handlerMethod.getParameterTypes();    // 聲明數(shù)組,存參數(shù)的值        Object[] args = new Object[paramTypes.length];    //2.遍歷參數(shù)數(shù)組,獲取每個(gè)參數(shù)的值        for (int i = 0; i < args.length; i++) {            MethodParameter methodParam = new MethodParameter(handlerMethod, i);            methodParam.initParameterNameDiscovery(this.parameterNameDiscoverer);            GenericTypeResolver.resolveParameterType(methodParam, handler.getClass());            String paramName = null;            String headerName = null;            boolean requestBodyFound = false;            String cookieName = null;            String pathVarName = null;            String attrName = null;            boolean required = false;            String defaultValue = null;            boolean validate = false;            int annotationsFound = 0;            Annotation[] paramAnns = methodParam.getParameterAnnotations();       // 處理參數(shù)上的注解            for (Annotation paramAnn : paramAnns) {                if (RequestParam.class.isInstance(paramAnn)) {                    RequestParam requestParam = (RequestParam) paramAnn;                    paramName = requestParam.value();                    required = requestParam.required();                    defaultValue = parseDefaultValueAttribute(requestParam.defaultValue());                    annotationsFound++;                }                else if (RequestHeader.class.isInstance(paramAnn)) {                    RequestHeader requestHeader = (RequestHeader) paramAnn;                    headerName = requestHeader.value();                    required = requestHeader.required();                    defaultValue = parseDefaultValueAttribute(requestHeader.defaultValue());                    annotationsFound++;                }                else if (RequestBody.class.isInstance(paramAnn)) {                    requestBodyFound = true;                    annotationsFound++;                }                else if (CookieValue.class.isInstance(paramAnn)) {                    CookieValue cookieValue = (CookieValue) paramAnn;                    cookieName = cookieValue.value();                    required = cookieValue.required();                    defaultValue = parseDefaultValueAttribute(cookieValue.defaultValue());                    annotationsFound++;                }                else if (PathVariable.class.isInstance(paramAnn)) {                    PathVariable pathVar = (PathVariable) paramAnn;                    pathVarName = pathVar.value();                    annotationsFound++;                }                else if (ModelAttribute.class.isInstance(paramAnn)) {                    ModelAttribute attr = (ModelAttribute) paramAnn;                    attrName = attr.value();                    annotationsFound++;                }                else if (Value.class.isInstance(paramAnn)) {                    defaultValue = ((Value) paramAnn).value();                }                else if ("Valid".equals(paramAnn.annotationType().getSimpleName())) {                    validate = true;                }            }             if (annotationsFound > 1) {                throw new IllegalStateException("Handler parameter annotations are exclusive choices - " +                        "do not specify more than one such annotation on the same parameter: " + handlerMethod);            }            if (annotationsFound == 0) {// 如果沒(méi)有注解                Object argValue = resolveCommonArgument(methodParam, webRequest);                if (argValue != WebArgumentResolver.UNRESOLVED) {                    args[i] = argValue;                }                else if (defaultValue != null) {                    args[i] = resolveDefaultValue(defaultValue);                }                else {                    Class paramType = methodParam.getParameterType();            // 將方法聲明中的Map和Model參數(shù),放到request中,用于將數(shù)據(jù)放到request中帶回頁(yè)面                    if (Model.class.isAssignableFrom(paramType) || Map.class.isAssignableFrom(paramType)) {                        args[i] = implicitModel;                    }                    else if (sessionStatus.class.isAssignableFrom(paramType)) {                        args[i] = this.sessionStatus;                    }                    else if (HttpEntity.class.isAssignableFrom(paramType)) {                        args[i] = resolveHttpEntityRequest(methodParam, webRequest);                    }                    else if (Errors.class.isAssignableFrom(paramType)) {                        throw new IllegalStateException("Errors/BindingResult argument declared " +                                "without preceding model attribute. Check your handler method signature!");                    }                    else if (BeanUtils.isSimpleProperty(paramType)) {                        paramName = "";                    }                    else {                        attrName = "";                    }                }            }       // 從request中取值,并進(jìn)行賦值操作            if (paramName != null) {         // 根據(jù)paramName從request中取值,如果沒(méi)有通過(guò)RequestParam注解指定paramName,則使用asm讀取class文件來(lái)獲取paramName                args[i] = resolveRequestParam(paramName, required, defaultValue, methodParam, webRequest, handler);            }            else if (headerName != null) {                args[i] = resolveRequestHeader(headerName, required, defaultValue, methodParam, webRequest, handler);            }            else if (requestBodyFound) {                args[i] = resolveRequestBody(methodParam, webRequest, handler);            }            else if (cookieName != null) {                args[i] = resolveCookieValue(cookieName, required, defaultValue, methodParam, webRequest, handler);            }            else if (pathVarName != null) {                args[i] = resolvePathVariable(pathVarName, methodParam, webRequest, handler);            }            else if (attrName != null) {                WebDataBinder binder =                        resolveModelAttribute(attrName, methodParam, implicitModel, webRequest, handler);                boolean assignBindingResult = (args.length > i + 1 && Errors.class.isAssignableFrom(paramTypes[i + 1]));                if (binder.getTarget() != null) {                    doBind(binder, webRequest, validate, !assignBindingResult);                }                args[i] = binder.getTarget();                if (assignBindingResult) {                    args[i + 1] = binder.getBindingResult();                    i++;                }                implicitModel.putAll(binder.getBindingResult().getModel());            }        }     // 返回參數(shù)值數(shù)組        return args;    }

 

resolveRequestParam方法將參數(shù)取出:

 

private Object resolveRequestParam (String paramName, boolean required, String defaultValue,                MethodParameter methodParam, NativeWebRequest webRequest, Object handlerForInitBinderCall)                 throws Exception {           Class<?> paramType = methodParam.getParameterType();            if (Map. class.isAssignableFrom(paramType) && paramName.length() == 0) {                 return resolveRequestParamMap((Class<? extends Map>) paramType, webRequest);           }            if (paramName.length() == 0) {                paramName = getRequiredParameterName(methodParam);           }           Object paramValue = null;           MultipartRequest multipartRequest = webRequest.getNativeRequest(MultipartRequest.class );            if (multipartRequest != null) {                List<MultipartFile> files = multipartRequest.getFiles(paramName);                 if (!files.isEmpty()) {                     paramValue = (files.size() == 1 ? files.get(0) : files);                }           }            if (paramValue == null) {               //數(shù)組參數(shù)執(zhí)行這行代碼,相當(dāng)于執(zhí)行了request.getParameterValues(name)                String[] paramValues = webRequest.getParameterValues(paramName);                 if (paramValues != null) {                    // 取出之后,如果是同名參數(shù)則賦值整個(gè)數(shù)組 paramValue此時(shí)是object                     paramValue = (paramValues. length == 1 ? paramValues[0] : paramValues);                }           }            if (paramValue == null) {                 if (defaultValue != null) {                     paramValue = resolveDefaultValue(defaultValue);                }                 else if (required) {                     raiseMissingParameterException(paramName, paramType);                }                paramValue = checkValue(paramName, paramValue, paramType);           }           WebDataBinder binder = createBinder(webRequest, null, paramName);           initBinder(handlerForInitBinderCall, paramName, binder, webRequest);             // 將paramValue根據(jù)參數(shù)類型進(jìn)行一次轉(zhuǎn)換操作            return binder.convertIfNecessary(paramValue, paramType, methodParam);     }

 

最終會(huì)執(zhí)行到TypeConverterDelegate的convertIfNecessary方法:

 

public <T> T convertIfNecessary(String propertyName, Object oldValue, Object newValue,                Class<T> requiredType, TypeDescriptor typeDescriptor) throws IllegalArgumentException {           Object convertedValue = newValue;            // Custom editor for this type?           PropertyEditor editor = this. propertyEditorRegistry.findCustomEditor(requiredType, propertyName);           ConversionFailedException firstAttemptEx = null;            // No custom editor but custom ConversionService specified?           ConversionService conversionService = this. propertyEditorRegistry.getConversionService();            if (editor == null && conversionService != null && convertedValue != null && typeDescriptor != null) {                TypeDescriptor sourceTypeDesc = TypeDescriptor.forObject(newValue);                TypeDescriptor targetTypeDesc = typeDescriptor;                 if (conversionService.canConvert(sourceTypeDesc, targetTypeDesc)) {                      try {                            return (T) conversionService.convert(convertedValue, sourceTypeDesc, targetTypeDesc);                     }                      catch (ConversionFailedException ex) {                            // fallback to default conversion logic below                           firstAttemptEx = ex;                     }                }           }            // Value not of required type?            if (editor != null || (requiredType != null && !ClassUtils.isAssignableValue(requiredType, convertedValue))) {                 if (requiredType != null && Collection.class.isAssignableFrom(requiredType) && convertedValue instanceof String) {                     TypeDescriptor elementType = typeDescriptor.getElementTypeDescriptor();                      if (elementType != null && Enum.class.isAssignableFrom(elementType.getType())) {                           convertedValue = StringUtils.commaDelimitedListToStringArray((String) convertedValue);                     }                }                 if (editor == null) {                     editor = findDefaultEditor(requiredType);                }                convertedValue = doConvertValue(oldValue, convertedValue, requiredType, editor);           }            boolean standardConversion = false;            if (requiredType != null) {                 // Try to apply some standard type conversion rules if appropriate.                 if (convertedValue != null) {                      // 因?yàn)槲覀兊膖ype是數(shù)組,所以執(zhí)行這段邏輯,他會(huì)執(zhí)行StringUtils.commaDelimitedListToStringArray                      if (requiredType.isArray()) {                            // Array required -> apply appropriate conversion of elements.                            if (convertedValue instanceof String && Enum.class.isAssignableFrom(requiredType.getComponentType())) {                                convertedValue = StringUtils.commaDelimitedListToStringArray((String) convertedValue);                           }                            return (T) convertToTypedArray(convertedValue, propertyName, requiredType.getComponentType());                     }                      else if (convertedValue instanceof Collection) {                            // Convert elements to target type, if determined.                           convertedValue = convertToTypedCollection(                                     (Collection) convertedValue, propertyName, requiredType, typeDescriptor);                           standardConversion = true;                     }                      else if (convertedValue instanceof Map) {                            // Convert keys and values to respective target type, if determined.                           convertedValue = convertToTypedMap(                                     (Map) convertedValue, propertyName, requiredType, typeDescriptor);                           standardConversion = true;                     }                      if (convertedValue.getClass().isArray() && Array.getLength(convertedValue) == 1) {                           convertedValue = Array.get(convertedValue, 0);                           standardConversion = true;                     }                      if (String. class.equals(requiredType) && ClassUtils.isPrimitiveOrWrapper(convertedValue.getClass())) {                            // We can stringify any primitive value...                            return (T) convertedValue.toString();                     }                      else if (convertedValue instanceof String && !requiredType.isInstance(convertedValue)) {                            if (firstAttemptEx == null && !requiredType.isInterface() && !requiredType.isEnum()) {                                 try {                                     Constructor strCtor = requiredType.getConstructor(String.class );                                      return (T) BeanUtils.instantiateClass(strCtor, convertedValue);                                }                                 catch (NoSuchMethodException ex) {                                      // proceed with field lookup                                      if (logger.isTraceEnabled()) {                                            logger.trace( "No String constructor found on type [" + requiredType.getName() + "]", ex);                                     }                                }                                 catch (Exception ex) {                                      if (logger.isDebugEnabled()) {                                           logger.debug( "Construction via String failed for type [" + requiredType.getName() + "]" , ex);                                     }                                }                           }                           String trimmedValue = ((String) convertedValue).trim();                            if (requiredType.isEnum() && "".equals(trimmedValue)) {                                 // It's an empty enum identifier: reset the enum value to null.                                 return null;                           }                           convertedValue = attemptToConvertStringToEnum(requiredType, trimmedValue, convertedValue);                           standardConversion = true;                     }                }                 if (!ClassUtils. isAssignableValue(requiredType, convertedValue)) {                      if (firstAttemptEx != null) {                            throw firstAttemptEx;                     }                      // Definitely doesn't match: throw IllegalArgumentException/IllegalStateException                     StringBuilder msg = new StringBuilder();                     msg.append( "Cannot convert value of type [").append(ClassUtils.getDescriptiveType (newValue));                     msg.append( "] to required type [").append(ClassUtils.getQualifiedName (requiredType)).append("]" );                      if (propertyName != null) {                           msg.append( " for property '").append(propertyName).append("'");                     }                      if (editor != null) {                           msg.append( ": PropertyEditor [").append(editor.getClass().getName()).append(                                      "] returned inappropriate value of type [").append(                                     ClassUtils. getDescriptiveType(convertedValue)).append( "]");                            throw new IllegalArgumentException(msg.toString());                     }                      else {                           msg.append( ": no matching editors or conversion strategy found");                            throw new IllegalStateException(msg.toString());                     }                }           }            if (firstAttemptEx != null) {                 if (editor == null && !standardConversion && requiredType != null && !Object.class.equals(requiredType)) {                      throw firstAttemptEx;                }                 logger.debug( "Original ConversionService attempt failed - ignored since " +                            "PropertyEditor based conversion eventually succeeded", firstAttemptEx);           }            return (T) convertedValue;     }

 

StringUtils.commaDelimitedListToStringArray代碼:

 

public static String[] commaDelimitedListToStringArray (String str) {            return  delimitedListToStringArray(str, ",");     }

 

最終執(zhí)行代碼,也就是哪里把這個(gè)逗號(hào)的參數(shù)區(qū)分成兩個(gè)參數(shù)的地方,眾里尋他千百度啊:

public static String[] delimitedListToStringArray (String str, String delimiter, String charsToDelete) {            if (str == null) {                 return new String[0];           }            if (delimiter == null) {                 return new String[] {str};           }           List<String> result = new ArrayList<String>();            if ( "".equals(delimiter)) {                 for ( int i = 0; i < str.length(); i++) {                     result.add( deleteAny(str.substring(i, i + 1), charsToDelete));                }           }            else {                 int pos = 0;                 int delPos;                // 逗號(hào)分隔,組裝新的list                 while ((delPos = str.indexOf(delimiter, pos)) != -1) {                     result.add( deleteAny(str.substring(pos, delPos), charsToDelete));                     pos = delPos + delimiter.length();                }                 if (str.length() > 0 && pos <= str.length()) {                      // Add rest of String, but not in case of empty input.                     result.add( deleteAny(str.substring(pos), charsToDelete));                }           }            return  toStringArray(result);     }

 

 

讓我們繼續(xù)前行

----------------------------------------------------------------------

努力不一定成功,但不努力肯定不會(huì)成功。

 

 


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 泰和县| 油尖旺区| 常州市| 印江| 略阳县| 嘉义县| 攀枝花市| 额济纳旗| 日照市| 大庆市| 灵台县| 阿拉尔市| 中西区| 平罗县| 青州市| 台东市| 饶河县| 安陆市| 布拖县| 新宁县| 西乡县| 启东市| 修文县| 大方县| 新昌县| 五家渠市| 河源市| 孙吴县| 砚山县| 海盐县| 龙岩市| 新郑市| 海南省| 兴和县| 阳泉市| 西和县| 铅山县| 沿河| 廊坊市| 黔江区| 台安县|