View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.web.servlet.mvc.method.annotation;
18  
19  import java.util.Map;
20  
21  import org.springframework.core.MethodParameter;
22  import org.springframework.ui.Model;
23  import org.springframework.ui.ModelMap;
24  import org.springframework.validation.DataBinder;
25  import org.springframework.web.bind.support.WebDataBinderFactory;
26  import org.springframework.web.context.request.NativeWebRequest;
27  import org.springframework.web.method.support.HandlerMethodArgumentResolver;
28  import org.springframework.web.method.support.ModelAndViewContainer;
29  import org.springframework.web.servlet.mvc.support.RedirectAttributes;
30  import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;
31  
32  /**
33   * Resolves method arguments of type {@link RedirectAttributes}.
34   *
35   * <p>This resolver must be listed ahead of
36   * {@link org.springframework.web.method.annotation.ModelMethodProcessor} and
37   * {@link org.springframework.web.method.annotation.MapMethodProcessor},
38   * which support {@link Map} and {@link Model} arguments both of which are
39   * "super" types of {@code RedirectAttributes} and would also attempt to
40   * resolve a {@code RedirectAttributes} argument.
41   *
42   * @author Rossen Stoyanchev
43   * @since 3.1
44   */
45  public class RedirectAttributesMethodArgumentResolver implements HandlerMethodArgumentResolver {
46  
47  	@Override
48  	public boolean supportsParameter(MethodParameter parameter) {
49  		return RedirectAttributes.class.isAssignableFrom(parameter.getParameterType());
50  	}
51  
52  	@Override
53  	public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
54  			NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
55  
56  		DataBinder dataBinder = binderFactory.createBinder(webRequest, null, null);
57  		ModelMap redirectAttributes  = new RedirectAttributesModelMap(dataBinder);
58  		mavContainer.setRedirectModel(redirectAttributes);
59  		return redirectAttributes;
60  	}
61  
62  }