SPR-7608 Add fallback mechanism for instantiating a model attribute from a path variable
This commit is contained in:
@@ -64,9 +64,9 @@ public class PathVariableMethodArgumentResolver extends AbstractNamedValueMethod
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception {
|
||||
String key = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
|
||||
int scope = RequestAttributes.SCOPE_REQUEST;
|
||||
Map<String, String> uriTemplateVars = (Map<String, String>) request.getAttribute(key, scope);
|
||||
Map<String, String> uriTemplateVars =
|
||||
(Map<String, String>) request.getAttribute(
|
||||
HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
|
||||
return (uriTemplateVars != null) ? uriTemplateVars.get(name) : null;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,17 +16,31 @@
|
||||
|
||||
package org.springframework.web.servlet.mvc.method.annotation.support;
|
||||
|
||||
import java.beans.PropertyEditor;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletRequest;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.validation.DataBinder;
|
||||
import org.springframework.web.bind.ServletRequestDataBinder;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.context.request.RequestAttributes;
|
||||
import org.springframework.web.method.annotation.support.ModelAttributeMethodProcessor;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
|
||||
/**
|
||||
* A Servlet-specific {@link ModelAttributeMethodProcessor} variant that casts the {@link WebDataBinder}
|
||||
* A Servlet-specific {@link ModelAttributeMethodProcessor} variant with the following further benefits:
|
||||
* <ul>
|
||||
* <li>Casts the data binder down to {@link ServletRequestDataBinder} prior to invoking bind on it
|
||||
* <li>Attempts to instantiate the model attribute using a path variable and type conversion
|
||||
* </ul>
|
||||
* that casts
|
||||
* instance to {@link ServletRequestDataBinder} prior to invoking data binding.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
@@ -44,12 +58,43 @@ public class ServletModelAttributeMethodProcessor extends ModelAttributeMethodPr
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p>This method downcasts the binder instance to {@link ServletRequestDataBinder} and invokes
|
||||
* its bind method passing a {@link ServletRequest} to it.
|
||||
* Instantiates the model attribute by trying to match the model attribute name to a path variable.
|
||||
* If a match is found an attempt is made to convert the String path variable to the expected
|
||||
* method parameter type through a registered {@link Converter} or {@link PropertyEditor}.
|
||||
* If this fails the call is delegated back to the parent for default constructor instantiation.
|
||||
*/
|
||||
@Override
|
||||
protected void doBind(WebDataBinder binder, NativeWebRequest request) {
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Object createAttribute(String attributeName,
|
||||
MethodParameter parameter,
|
||||
WebDataBinderFactory binderFactory,
|
||||
NativeWebRequest request) throws Exception {
|
||||
Map<String, String> uriTemplateVars =
|
||||
(Map<String, String>) request.getAttribute(
|
||||
HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
|
||||
|
||||
if (uriTemplateVars != null && uriTemplateVars.containsKey(attributeName)) {
|
||||
try {
|
||||
String var = uriTemplateVars.get(attributeName);
|
||||
DataBinder binder = binderFactory.createBinder(request, null, attributeName);
|
||||
return binder.convertIfNecessary(var, parameter.getParameterType());
|
||||
|
||||
} catch (Exception exception) {
|
||||
logger.info("Model attribute '" + attributeName + "' matches to a URI template variable name. "
|
||||
+ "The URI template variable however couldn't converted to a model attribute instance: "
|
||||
+ exception.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return super.createAttribute(attributeName, parameter, binderFactory, request);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p>This implementation downcasts to {@link ServletRequestDataBinder} before invoking the bind operation.
|
||||
*/
|
||||
@Override
|
||||
protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
|
||||
ServletRequest servletRequest = request.getNativeRequest(ServletRequest.class);
|
||||
ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
|
||||
servletBinder.bind(servletRequest);
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* Copyright 2002-2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.mvc.method.annotation;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.support.ServletModelAttributeMethodProcessor;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link ServletModelAttributeMethodProcessor} specific tests.
|
||||
* Also see org.springframework.web.method.annotation.support.ModelAttributeMethodProcessorTests
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class SerlvetModelAttributeMethodProcessorTests {
|
||||
|
||||
private ServletModelAttributeMethodProcessor processor;
|
||||
|
||||
private MethodParameter testBeanModelAttr;
|
||||
|
||||
private MethodParameter testBeanWithoutStringConstructorModelAttr;
|
||||
|
||||
private ModelAndViewContainer mavContainer;
|
||||
|
||||
private NativeWebRequest webRequest;
|
||||
|
||||
private MockHttpServletRequest request;
|
||||
|
||||
private WebDataBinderFactory binderFactory;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
processor = new ServletModelAttributeMethodProcessor(false);
|
||||
|
||||
Method method = getClass().getDeclaredMethod("modelAttribute",
|
||||
TestBean.class, TestBeanWithoutStringConstructor.class);
|
||||
|
||||
testBeanModelAttr = new MethodParameter(method, 0);
|
||||
testBeanWithoutStringConstructorModelAttr = new MethodParameter(method, 1);
|
||||
|
||||
binderFactory = new ServletRequestDataBinderFactory(null, null);
|
||||
mavContainer = new ModelAndViewContainer();
|
||||
|
||||
request = new MockHttpServletRequest();
|
||||
webRequest = new ServletWebRequest(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createAttributeViaPathVariable() throws Exception {
|
||||
Map<String, String> uriTemplateVars = new HashMap<String, String>();
|
||||
uriTemplateVars.put("testBean1", "pathy");
|
||||
request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);
|
||||
|
||||
// Type conversion from "pathy" to TestBean via TestBean(String) constructor
|
||||
|
||||
TestBean testBean =
|
||||
(TestBean) processor.resolveArgument(testBeanModelAttr, mavContainer, webRequest, binderFactory);
|
||||
|
||||
assertEquals("pathy", testBean.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createAttributeAfterPathVariableConversionError() throws Exception {
|
||||
Map<String, String> uriTemplateVars = new HashMap<String, String>();
|
||||
uriTemplateVars.put("testBean1", "pathy");
|
||||
request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);
|
||||
|
||||
TestBeanWithoutStringConstructor testBean =
|
||||
(TestBeanWithoutStringConstructor) processor.resolveArgument(
|
||||
testBeanWithoutStringConstructorModelAttr, mavContainer, webRequest, binderFactory);
|
||||
|
||||
assertNotNull(testBean);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private void modelAttribute(@ModelAttribute("testBean1") TestBean testBean1,
|
||||
@ModelAttribute("testBean2") TestBeanWithoutStringConstructor testBean2) {
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class TestBeanWithoutStringConstructor {
|
||||
|
||||
public TestBeanWithoutStringConstructor() {
|
||||
}
|
||||
|
||||
public TestBeanWithoutStringConstructor(int i) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user