MultiForm表单后台处理方式

首先确定下的是MultiForm的表单命名是FieldName + _o_数字


后台在处理改类型的Form的时候,一般会进行如下代码
UtilHttp
.parseMultiFormData(UtilHttp.getParameterMap(request));
UtilHttp.getParameterMap(request)这里不是重点,这里只是将请求参数转换成需要的Map格式,重要看parseMultiFormData,它是处理MultiForm表单请求数据的。
进入

public static Collection<Map<String, Object>> parseMultiFormData(Map<String, Object> parameters) {
//rows,请求参数中哪些参数会被保留由它决定
        FastMap<Integer, Map<String, Object>> rows = FastMap.newInstance(); // stores the rows keyed by row number

        //开始处理每个参数
        for (String key: parameters.keySet()) {
            //这里key==null不用看就知道,key.length() <= ROW_SUBMIT_PREFIX_LENGTH这里的ROW_SUBMIT_PREFIX_LENGTH值是13,为神马是13呢?这是因为它将_rowSubmit_o_开头的参数作为判断该请求参数是否有效,如_rowSubmit_o_1=Y,那么所有FieldName + _o_1的参数都是有效地。_rowSubmit_o_是13个字符。
            if (key == null || key.length() <= ROW_SUBMIT_PREFIX_LENGTH) continue;
//检查是否存在MULTI_ROW_DELIMITER(_o_)
            if (key.indexOf(MULTI_ROW_DELIMITER) <= 0) continue;
            if (!key.substring(0, ROW_SUBMIT_PREFIX_LENGTH).equals(ROW_SUBMIT_PREFIX)) continue;
//检查值是否Y
            if (!parameters.get(key).equals("Y")) continue;

//计算出它的数字
            // decode the value of N and create a new map for it
            Integer n = Integer.decode(key.substring(ROW_SUBMIT_PREFIX_LENGTH, key.length()));
            Map<String, Object> m = FastMap.newInstance();
//得出所有有效地数字。
//这里我们还可以知道通过row可以取出数字
            m.put("row", n); // special "row" = N tuple
            rows.put(n, m); // key it to N
        }

        // 只保留有效地参数
        for (String key: parameters.keySet()) {
            // skip keys without DELIMITER and skip ROW_SUBMIT_PREFIX
            if (key == null) continue;
            int index = key.indexOf(MULTI_ROW_DELIMITER);
            if (index <= 0) continue;
            if (key.length() > ROW_SUBMIT_PREFIX_LENGTH && key.substring(0, ROW_SUBMIT_PREFIX_LENGTH).equals(ROW_SUBMIT_PREFIX)) continue;

            // get the map with index N
            Integer n = Integer.decode(key.substring(index + MULTI_ROW_DELIMITER_LENGTH, key.length())); // N from ${param}${DELIMITER}${N}
            Map<String, Object> map = rows.get(n);
            if (map == null) continue;

            // 这里最后得到的是哪些数字是有效地,这些数字有哪些参数。
            String newKey = key.substring(0, index);
            map.put(newKey, parameters.get(key));
        }
        // return only the values, which is the list of maps
        return rows.values();
    }

你可能感兴趣的:(form)