001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.bind;
018
019import javax.servlet.ServletRequest;
020
021import org.springframework.beans.MutablePropertyValues;
022import org.springframework.lang.Nullable;
023import org.springframework.web.util.WebUtils;
024
025/**
026 * PropertyValues implementation created from parameters in a ServletRequest.
027 * Can look for all property values beginning with a certain prefix and
028 * prefix separator (default is "_").
029 *
030 * <p>For example, with a prefix of "spring", "spring_param1" and
031 * "spring_param2" result in a Map with "param1" and "param2" as keys.
032 *
033 * <p>This class is not immutable to be able to efficiently remove property
034 * values that should be ignored for binding.
035 *
036 * @author Rod Johnson
037 * @author Juergen Hoeller
038 * @see org.springframework.web.util.WebUtils#getParametersStartingWith
039 */
040@SuppressWarnings("serial")
041public class ServletRequestParameterPropertyValues extends MutablePropertyValues {
042
043        /** Default prefix separator. */
044        public static final String DEFAULT_PREFIX_SEPARATOR = "_";
045
046
047        /**
048         * Create new ServletRequestPropertyValues using no prefix
049         * (and hence, no prefix separator).
050         * @param request the HTTP request
051         */
052        public ServletRequestParameterPropertyValues(ServletRequest request) {
053                this(request, null, null);
054        }
055
056        /**
057         * Create new ServletRequestPropertyValues using the given prefix and
058         * the default prefix separator (the underscore character "_").
059         * @param request the HTTP request
060         * @param prefix the prefix for parameters (the full prefix will
061         * consist of this plus the separator)
062         * @see #DEFAULT_PREFIX_SEPARATOR
063         */
064        public ServletRequestParameterPropertyValues(ServletRequest request, @Nullable String prefix) {
065                this(request, prefix, DEFAULT_PREFIX_SEPARATOR);
066        }
067
068        /**
069         * Create new ServletRequestPropertyValues supplying both prefix and
070         * prefix separator.
071         * @param request the HTTP request
072         * @param prefix the prefix for parameters (the full prefix will
073         * consist of this plus the separator)
074         * @param prefixSeparator separator delimiting prefix (e.g. "spring")
075         * and the rest of the parameter name ("param1", "param2")
076         */
077        public ServletRequestParameterPropertyValues(
078                        ServletRequest request, @Nullable String prefix, @Nullable String prefixSeparator) {
079
080                super(WebUtils.getParametersStartingWith(
081                                request, (prefix != null ? prefix + prefixSeparator : null)));
082        }
083
084}