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