001/*
002 * Copyright 2002-2012 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.web.util.WebUtils;
023
024/**
025 * PropertyValues implementation created from parameters in a ServletRequest.
026 * Can look for all property values beginning with a certain prefix and
027 * prefix separator (default is "_").
028 *
029 * <p>For example, with a prefix of "spring", "spring_param1" and
030 * "spring_param2" result in a Map with "param1" and "param2" as keys.
031 *
032 * <p>This class is not immutable to be able to efficiently remove property
033 * values that should be ignored for binding.
034 *
035 * @author Rod Johnson
036 * @author Juergen Hoeller
037 * @see org.springframework.web.util.WebUtils#getParametersStartingWith
038 */
039@SuppressWarnings("serial")
040public class ServletRequestParameterPropertyValues extends MutablePropertyValues {
041
042        /** Default prefix separator */
043        public static final String DEFAULT_PREFIX_SEPARATOR = "_";
044
045
046        /**
047         * Create new ServletRequestPropertyValues using no prefix
048         * (and hence, no prefix separator).
049         * @param request HTTP request
050         */
051        public ServletRequestParameterPropertyValues(ServletRequest request) {
052                this(request, null, null);
053        }
054
055        /**
056         * Create new ServletRequestPropertyValues using the given prefix and
057         * the default prefix separator (the underscore character "_").
058         * @param request HTTP request
059         * @param prefix the prefix for parameters (the full prefix will
060         * consist of this plus the separator)
061         * @see #DEFAULT_PREFIX_SEPARATOR
062         */
063        public ServletRequestParameterPropertyValues(ServletRequest request, String prefix) {
064                this(request, prefix, DEFAULT_PREFIX_SEPARATOR);
065        }
066
067        /**
068         * Create new ServletRequestPropertyValues supplying both prefix and
069         * prefix separator.
070         * @param request HTTP request
071         * @param prefix the prefix for parameters (the full prefix will
072         * consist of this plus the separator)
073         * @param prefixSeparator separator delimiting prefix (e.g. "spring")
074         * and the rest of the parameter name ("param1", "param2")
075         */
076        public ServletRequestParameterPropertyValues(ServletRequest request, String prefix, String prefixSeparator) {
077                super(WebUtils.getParametersStartingWith(
078                                request, (prefix != null ? prefix + prefixSeparator : null)));
079        }
080
081}