SPR-6909 Include URI template vars in data binding
This commit is contained in:
@@ -42,6 +42,7 @@ import org.springframework.http.converter.StringHttpMessageConverter;
|
||||
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
|
||||
import org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter;
|
||||
import org.springframework.util.ReflectionUtils.MethodFilter;
|
||||
import org.springframework.validation.DataBinder;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.annotation.InitBinder;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
@@ -257,13 +258,19 @@ public class RequestMappingHandlerMethodAdapter extends AbstractHandlerMethodAda
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify a WebBindingInitializer which will apply pre-configured
|
||||
* configuration to every DataBinder that this controller uses.
|
||||
* Set a WebBindingInitializer to apply configure every DataBinder instance this controller uses.
|
||||
*/
|
||||
public void setWebBindingInitializer(WebBindingInitializer webBindingInitializer) {
|
||||
this.webBindingInitializer = webBindingInitializer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the WebBindingInitializer which applies pre-configured configuration to {@link DataBinder} instances.
|
||||
*/
|
||||
public WebBindingInitializer getWebBindingInitializer() {
|
||||
return webBindingInitializer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the strategy to store session attributes with.
|
||||
* <p>Default is {@link org.springframework.web.bind.support.DefaultSessionAttributeStore},
|
||||
|
||||
@@ -17,12 +17,17 @@
|
||||
package org.springframework.web.servlet.mvc.method.annotation;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.web.bind.ServletRequestDataBinder;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.support.WebBindingInitializer;
|
||||
import org.springframework.web.context.request.RequestAttributes;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.method.annotation.InitBinderMethodDataBinderFactory;
|
||||
import org.springframework.web.method.support.InvocableHandlerMethod;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
|
||||
/**
|
||||
* An {@link InitBinderMethodDataBinderFactory} that creates a {@link ServletRequestDataBinder}.
|
||||
@@ -47,7 +52,30 @@ public class ServletInitBinderMethodDataBinderFactory extends InitBinderMethodDa
|
||||
*/
|
||||
@Override
|
||||
protected WebDataBinder createBinderInstance(Object target, String objectName) {
|
||||
return new ServletRequestDataBinder(target, objectName);
|
||||
return new ServletRequestPathVarDataBinder(target, objectName);
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* Adds URI template variables to the map of request values used to do data binding.
|
||||
*/
|
||||
private static class ServletRequestPathVarDataBinder extends ServletRequestDataBinder {
|
||||
|
||||
public ServletRequestPathVarDataBinder(Object target, String objectName) {
|
||||
super(target, objectName);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
protected void doBind(MutablePropertyValues mpvs) {
|
||||
RequestAttributes requestAttrs = RequestContextHolder.getRequestAttributes();
|
||||
if (requestAttrs != null) {
|
||||
String key = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
|
||||
int scope = RequestAttributes.SCOPE_REQUEST;
|
||||
Map<String, String> uriTemplateVars = (Map<String, String>) requestAttrs.getAttribute(key, scope);
|
||||
mpvs.addPropertyValues(uriTemplateVars);
|
||||
}
|
||||
super.doBind(mpvs);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user