Allow configuration of custom redirect patterns

This change enables the ability to configure
ViewNameMethodReturnValueHandler & ModelAndViewMethodReturnValueHandler
with patterns to use to test for a custom redirect view name.

Issue: SPR-12054
This commit is contained in:
Rossen Stoyanchev
2014-08-25 16:12:03 -04:00
parent 2a0765e76d
commit 8f715a8547
4 changed files with 126 additions and 21 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.web.servlet.mvc.method.annotation;
import org.springframework.core.MethodParameter;
import org.springframework.util.PatternMatchUtils;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;
@@ -42,6 +43,32 @@ import org.springframework.web.servlet.View;
*/
public class ModelAndViewMethodReturnValueHandler implements HandlerMethodReturnValueHandler {
private String[] redirectPatterns;
/**
* Configure one more simple patterns (as described in
* {@link org.springframework.util.PatternMatchUtils#simpleMatch}) to use in order to recognize
* custom redirect prefixes in addition to "redirect:".
*
* <p>Note that simply configuring this property will not make a custom
* redirect prefix work. There must be a custom View that recognizes the
* prefix as well.
*
* @since 4.1
*/
public void setRedirectPatterns(String... redirectPatterns) {
this.redirectPatterns = redirectPatterns;
}
/**
* The configured redirect patterns, if any.
*/
public String[] getRedirectPatterns() {
return this.redirectPatterns;
}
@Override
public boolean supportsReturnType(MethodParameter returnType) {
return ModelAndView.class.isAssignableFrom(returnType.getParameterType());
@@ -62,7 +89,7 @@ public class ModelAndViewMethodReturnValueHandler implements HandlerMethodReturn
if (mav.isReference()) {
String viewName = mav.getViewName();
mavContainer.setViewName(viewName);
if (viewName != null && viewName.startsWith("redirect:")) {
if (viewName != null && isRedirectViewName(viewName)) {
mavContainer.setRedirectModelScenario(true);
}
}
@@ -78,4 +105,19 @@ public class ModelAndViewMethodReturnValueHandler implements HandlerMethodReturn
mavContainer.addAllAttributes(mav.getModel());
}
/**
* Whether the given view name is a redirect view reference.
* The default implementation checks the configured redirect patterns and
* also if the view name starts with the "redirect:" prefix.
* @param viewName the view name to check, never {@code null}
* @return "true" if the given view name is recognized as a redirect view
* reference; "false" otherwise.
*/
protected boolean isRedirectViewName(String viewName) {
if (PatternMatchUtils.simpleMatch(this.redirectPatterns, viewName)) {
return true;
}
return viewName.startsWith("redirect:");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.web.servlet.mvc.method.annotation;
import org.springframework.core.MethodParameter;
import org.springframework.util.PatternMatchUtils;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;
@@ -40,6 +41,31 @@ import org.springframework.web.servlet.RequestToViewNameTranslator;
*/
public class ViewNameMethodReturnValueHandler implements HandlerMethodReturnValueHandler {
private String[] redirectPatterns;
/**
* Configure one more simple patterns (as described in
* {@link PatternMatchUtils#simpleMatch}) to use in order to recognize
* custom redirect prefixes in addition to "redirect:".
*
* <p>Note that simply configuring this property will not make a custom
* redirect prefix work. There must be a custom View that recognizes the
* prefix as well.
*
* @since 4.1
*/
public void setRedirectPatterns(String... redirectPatterns) {
this.redirectPatterns = redirectPatterns;
}
/**
* The configured redirect patterns, if any.
*/
public String[] getRedirectPatterns() {
return this.redirectPatterns;
}
@Override
public boolean supportsReturnType(MethodParameter returnType) {
Class<?> paramType = returnType.getParameterType();
@@ -71,11 +97,16 @@ public class ViewNameMethodReturnValueHandler implements HandlerMethodReturnValu
/**
* Whether the given view name is a redirect view reference.
* The default implementation checks the configured redirect patterns and
* also if the view name starts with the "redirect:" prefix.
* @param viewName the view name to check, never {@code null}
* @return "true" if the given view name is recognized as a redirect view
* reference; "false" otherwise.
*/
protected boolean isRedirectViewName(String viewName) {
if (PatternMatchUtils.simpleMatch(this.redirectPatterns, viewName)) {
return true;
}
return viewName.startsWith("redirect:");
}