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.servlet.mvc.support;
018
019import java.util.Collection;
020import java.util.Map;
021
022import org.springframework.ui.Model;
023import org.springframework.web.servlet.FlashMap;
024
025/**
026 * A specialization of the {@link Model} interface that controllers can use to
027 * select attributes for a redirect scenario. Since the intent of adding
028 * redirect attributes is very explicit --  i.e. to be used for a redirect URL,
029 * attribute values may be formatted as Strings and stored that way to make
030 * them eligible to be appended to the query string or expanded as URI
031 * variables in {@code org.springframework.web.servlet.view.RedirectView}.
032 *
033 * <p>This interface also provides a way to add flash attributes. For a
034 * general overview of flash attributes see {@link FlashMap}. You can use
035 * {@link RedirectAttributes} to store flash attributes and they will be
036 * automatically propagated to the "output" FlashMap of the current request.
037 *
038 * <p>Example usage in an {@code @Controller}:
039 * <pre class="code">
040 * &#064;RequestMapping(value = "/accounts", method = RequestMethod.POST)
041 * public String handle(Account account, BindingResult result, RedirectAttributes redirectAttrs) {
042 *   if (result.hasErrors()) {
043 *     return "accounts/new";
044 *   }
045 *   // Save account ...
046 *   redirectAttrs.addAttribute("id", account.getId()).addFlashAttribute("message", "Account created!");
047 *   return "redirect:/accounts/{id}";
048 * }
049 * </pre>
050 *
051 * <p>A RedirectAttributes model is empty when the method is called and is never
052 * used unless the method returns a redirect view name or a RedirectView.
053 *
054 * <p>After the redirect, flash attributes are automatically added to the model
055 * of the controller that serves the target URL.
056 *
057 * @author Rossen Stoyanchev
058 * @since 3.1
059 */
060public interface RedirectAttributes extends Model {
061
062        @Override
063        RedirectAttributes addAttribute(String attributeName, Object attributeValue);
064
065        @Override
066        RedirectAttributes addAttribute(Object attributeValue);
067
068        @Override
069        RedirectAttributes addAllAttributes(Collection<?> attributeValues);
070
071        @Override
072        RedirectAttributes mergeAttributes(Map<String, ?> attributes);
073
074        /**
075         * Add the given flash attribute.
076         * @param attributeName the attribute name; never {@code null}
077         * @param attributeValue the attribute value; may be {@code null}
078         */
079        RedirectAttributes addFlashAttribute(String attributeName, Object attributeValue);
080
081        /**
082         * Add the given flash storage using a
083         * {@link org.springframework.core.Conventions#getVariableName generated name}.
084         * @param attributeValue the flash attribute value; never {@code null}
085         */
086        RedirectAttributes addFlashAttribute(Object attributeValue);
087
088        /**
089         * Return the attributes candidate for flash storage or an empty Map.
090         */
091        Map<String, ?> getFlashAttributes();
092}