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 org.springframework.core.MethodParameter;
20  import org.springframework.util.PatternMatchUtils;
21  import org.springframework.web.context.request.NativeWebRequest;
22  import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
23  import org.springframework.web.method.support.ModelAndViewContainer;
24  import org.springframework.web.servlet.ModelAndView;
25  import org.springframework.web.servlet.SmartView;
26  import org.springframework.web.servlet.View;
27  
28  /**
29   * Handles return values of type {@link ModelAndView} copying view and model
30   * information to the {@link ModelAndViewContainer}.
31   *
32   * <p>If the return value is {@code null}, the
33   * {@link ModelAndViewContainer#setRequestHandled(boolean)} flag is set to
34   * {@code true} to indicate the request was handled directly.
35   *
36   * <p>A {@link ModelAndView} return type has a set purpose. Therefore this
37   * handler should be configured ahead of handlers that support any return
38   * value type annotated with {@code @ModelAttribute} or {@code @ResponseBody}
39   * to ensure they don't take over.
40   *
41   * @author Rossen Stoyanchev
42   * @since 3.1
43   */
44  public class ModelAndViewMethodReturnValueHandler implements HandlerMethodReturnValueHandler {
45  
46  	private String[] redirectPatterns;
47  
48  
49  	/**
50  	 * Configure one more simple patterns (as described in
51  	 * {@link org.springframework.util.PatternMatchUtils#simpleMatch}) to use in order to recognize
52  	 * custom redirect prefixes in addition to "redirect:".
53  	 * <p>Note that simply configuring this property will not make a custom
54  	 * redirect prefix work. There must be a custom View that recognizes the
55  	 * prefix as well.
56  	 * @since 4.1
57  	 */
58  	public void setRedirectPatterns(String... redirectPatterns) {
59  		this.redirectPatterns = redirectPatterns;
60  	}
61  
62  	/**
63  	 * The configured redirect patterns, if any.
64  	 */
65  	public String[] getRedirectPatterns() {
66  		return this.redirectPatterns;
67  	}
68  
69  
70  	@Override
71  	public boolean supportsReturnType(MethodParameter returnType) {
72  		return ModelAndView.class.isAssignableFrom(returnType.getParameterType());
73  	}
74  
75  	@Override
76  	public void handleReturnValue(Object returnValue, MethodParameter returnType,
77  			ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
78  
79  		if (returnValue == null) {
80  			mavContainer.setRequestHandled(true);
81  			return;
82  		}
83  
84  		ModelAndView mav = (ModelAndView) returnValue;
85  		if (mav.isReference()) {
86  			String viewName = mav.getViewName();
87  			mavContainer.setViewName(viewName);
88  			if (viewName != null && isRedirectViewName(viewName)) {
89  				mavContainer.setRedirectModelScenario(true);
90  			}
91  		}
92  		else {
93  			View view = mav.getView();
94  			mavContainer.setView(view);
95  			if (view instanceof SmartView) {
96  				if (((SmartView) view).isRedirectView()) {
97  					mavContainer.setRedirectModelScenario(true);
98  				}
99  			}
100 		}
101 		mavContainer.addAllAttributes(mav.getModel());
102 	}
103 
104 	/**
105 	 * Whether the given view name is a redirect view reference.
106 	 * The default implementation checks the configured redirect patterns and
107 	 * also if the view name starts with the "redirect:" prefix.
108 	 * @param viewName the view name to check, never {@code null}
109 	 * @return "true" if the given view name is recognized as a redirect view
110 	 * reference; "false" otherwise.
111 	 */
112 	protected boolean isRedirectViewName(String viewName) {
113 		if (PatternMatchUtils.simpleMatch(this.redirectPatterns, viewName)) {
114 			return true;
115 		}
116 		return viewName.startsWith("redirect:");
117 	}
118 
119 }