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:
@@ -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:");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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:");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user