001/* 002 * Copyright 2002-2019 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.method.annotation; 018 019import org.springframework.core.MethodParameter; 020import org.springframework.lang.Nullable; 021import org.springframework.util.PatternMatchUtils; 022import org.springframework.web.context.request.NativeWebRequest; 023import org.springframework.web.method.support.HandlerMethodReturnValueHandler; 024import org.springframework.web.method.support.ModelAndViewContainer; 025import org.springframework.web.servlet.RequestToViewNameTranslator; 026 027/** 028 * Handles return values of types {@code void} and {@code String} interpreting them 029 * as view name reference. As of 4.2, it also handles general {@code CharSequence} 030 * types, e.g. {@code StringBuilder} or Groovy's {@code GString}, as view names. 031 * 032 * <p>A {@code null} return value, either due to a {@code void} return type or 033 * as the actual return value is left as-is allowing the configured 034 * {@link RequestToViewNameTranslator} to select a view name by convention. 035 * 036 * <p>A String return value can be interpreted in more than one ways depending on 037 * the presence of annotations like {@code @ModelAttribute} or {@code @ResponseBody}. 038 * Therefore this handler should be configured after the handlers that support these 039 * annotations. 040 * 041 * @author Rossen Stoyanchev 042 * @author Juergen Hoeller 043 * @since 3.1 044 */ 045public class ViewNameMethodReturnValueHandler implements HandlerMethodReturnValueHandler { 046 047 @Nullable 048 private String[] redirectPatterns; 049 050 051 /** 052 * Configure one more simple patterns (as described in {@link PatternMatchUtils#simpleMatch}) 053 * to use in order to recognize custom redirect prefixes in addition to "redirect:". 054 * <p>Note that simply configuring this property will not make a custom redirect prefix work. 055 * There must be a custom View that recognizes the prefix as well. 056 * @since 4.1 057 */ 058 public void setRedirectPatterns(@Nullable String... redirectPatterns) { 059 this.redirectPatterns = redirectPatterns; 060 } 061 062 /** 063 * The configured redirect patterns, if any. 064 */ 065 @Nullable 066 public String[] getRedirectPatterns() { 067 return this.redirectPatterns; 068 } 069 070 071 @Override 072 public boolean supportsReturnType(MethodParameter returnType) { 073 Class<?> paramType = returnType.getParameterType(); 074 return (void.class == paramType || CharSequence.class.isAssignableFrom(paramType)); 075 } 076 077 @Override 078 public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, 079 ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception { 080 081 if (returnValue instanceof CharSequence) { 082 String viewName = returnValue.toString(); 083 mavContainer.setViewName(viewName); 084 if (isRedirectViewName(viewName)) { 085 mavContainer.setRedirectModelScenario(true); 086 } 087 } 088 else if (returnValue != null) { 089 // should not happen 090 throw new UnsupportedOperationException("Unexpected return type: " + 091 returnType.getParameterType().getName() + " in method: " + returnType.getMethod()); 092 } 093 } 094 095 /** 096 * Whether the given view name is a redirect view reference. 097 * The default implementation checks the configured redirect patterns and 098 * also if the view name starts with the "redirect:" prefix. 099 * @param viewName the view name to check, never {@code null} 100 * @return "true" if the given view name is recognized as a redirect view 101 * reference; "false" otherwise. 102 */ 103 protected boolean isRedirectViewName(String viewName) { 104 return (PatternMatchUtils.simpleMatch(this.redirectPatterns, viewName) || viewName.startsWith("redirect:")); 105 } 106 107}