SPR-7608 Add fallback mechanism for instantiating a model attribute from a path variable
This commit is contained in:
parent
8e240d814b
commit
1e07af8827
|
|
@ -64,9 +64,9 @@ public class PathVariableMethodArgumentResolver extends AbstractNamedValueMethod
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception {
|
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception {
|
||||||
String key = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
|
Map<String, String> uriTemplateVars =
|
||||||
int scope = RequestAttributes.SCOPE_REQUEST;
|
(Map<String, String>) request.getAttribute(
|
||||||
Map<String, String> uriTemplateVars = (Map<String, String>) request.getAttribute(key, scope);
|
HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
|
||||||
return (uriTemplateVars != null) ? uriTemplateVars.get(name) : null;
|
return (uriTemplateVars != null) ? uriTemplateVars.get(name) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,17 +16,31 @@
|
||||||
|
|
||||||
package org.springframework.web.servlet.mvc.method.annotation.support;
|
package org.springframework.web.servlet.mvc.method.annotation.support;
|
||||||
|
|
||||||
|
import java.beans.PropertyEditor;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.servlet.ServletRequest;
|
import javax.servlet.ServletRequest;
|
||||||
|
|
||||||
import org.springframework.beans.BeanUtils;
|
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.ServletRequestDataBinder;
|
||||||
import org.springframework.web.bind.WebDataBinder;
|
import org.springframework.web.bind.WebDataBinder;
|
||||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
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.NativeWebRequest;
|
||||||
|
import org.springframework.web.context.request.RequestAttributes;
|
||||||
import org.springframework.web.method.annotation.support.ModelAttributeMethodProcessor;
|
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.
|
* instance to {@link ServletRequestDataBinder} prior to invoking data binding.
|
||||||
*
|
*
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
|
|
@ -44,12 +58,43 @@ public class ServletModelAttributeMethodProcessor extends ModelAttributeMethodPr
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* Instantiates the model attribute by trying to match the model attribute name to a path variable.
|
||||||
* <p>This method downcasts the binder instance to {@link ServletRequestDataBinder} and invokes
|
* If a match is found an attempt is made to convert the String path variable to the expected
|
||||||
* its bind method passing a {@link ServletRequest} to it.
|
* 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
|
@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);
|
ServletRequest servletRequest = request.getNativeRequest(ServletRequest.class);
|
||||||
ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
|
ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
|
||||||
servletBinder.bind(servletRequest);
|
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) {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -18,11 +18,12 @@ package org.springframework.web.method.annotation.support;
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.core.MethodParameter;
|
import org.springframework.core.MethodParameter;
|
||||||
import org.springframework.validation.BindException;
|
import org.springframework.validation.BindException;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.validation.DataBinder;
|
|
||||||
import org.springframework.validation.Errors;
|
import org.springframework.validation.Errors;
|
||||||
import org.springframework.web.bind.WebDataBinder;
|
import org.springframework.web.bind.WebDataBinder;
|
||||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
|
@ -50,6 +51,8 @@ import org.springframework.web.method.support.ModelAndViewContainer;
|
||||||
*/
|
*/
|
||||||
public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResolver, HandlerMethodReturnValueHandler {
|
public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResolver, HandlerMethodReturnValueHandler {
|
||||||
|
|
||||||
|
protected Log logger = LogFactory.getLog(this.getClass());
|
||||||
|
|
||||||
private final boolean useDefaultResolution;
|
private final boolean useDefaultResolution;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -82,26 +85,33 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol
|
||||||
* default constructor. Data binding and optionally validation is then applied through a {@link WebDataBinder}
|
* default constructor. Data binding and optionally validation is then applied through a {@link WebDataBinder}
|
||||||
* instance. Validation is invoked optionally when the method parameter is annotated with an {@code @Valid}.
|
* instance. Validation is invoked optionally when the method parameter is annotated with an {@code @Valid}.
|
||||||
*
|
*
|
||||||
* @throws Exception if a {@link WebDataBinder} could not be created or if data binding and validation result in
|
* @throws BindException if data binding and validation result in an error and the next method parameter
|
||||||
* an error and the next method parameter is not of type {@link Errors} or {@link BindingResult}.
|
* is neither of type {@link Errors} nor {@link BindingResult}.
|
||||||
|
* @throws Exception if a {@link WebDataBinder} could not be created.
|
||||||
*/
|
*/
|
||||||
public final Object resolveArgument(MethodParameter parameter,
|
public final Object resolveArgument(MethodParameter parameter,
|
||||||
ModelAndViewContainer mavContainer,
|
ModelAndViewContainer mavContainer,
|
||||||
NativeWebRequest webRequest,
|
NativeWebRequest request,
|
||||||
WebDataBinderFactory binderFactory) throws Exception {
|
WebDataBinderFactory binderFactory) throws Exception {
|
||||||
WebDataBinder binder = createDataBinder(parameter, mavContainer, webRequest, binderFactory);
|
String name = ModelFactory.getNameForParameter(parameter);
|
||||||
|
Object target = (mavContainer.containsAttribute(name)) ?
|
||||||
|
mavContainer.getAttribute(name) : createAttribute(name, parameter, binderFactory, request);
|
||||||
|
|
||||||
|
WebDataBinder binder = binderFactory.createBinder(request, target, name);
|
||||||
|
|
||||||
if (binder.getTarget() != null) {
|
if (binder.getTarget() != null) {
|
||||||
doBind(binder, webRequest);
|
bindRequestParameters(binder, request);
|
||||||
|
|
||||||
if (shouldValidate(binder, parameter)) {
|
if (isValidationApplicable(binder, parameter)) {
|
||||||
binder.validate();
|
binder.validate();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (failOnError(binder, parameter) && binder.getBindingResult().hasErrors()) {
|
if (binder.getBindingResult().hasErrors()) {
|
||||||
|
if (isBindExceptionRequired(binder, parameter)) {
|
||||||
throw new BindException(binder.getBindingResult());
|
throw new BindException(binder.getBindingResult());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mavContainer.addAllAttributes(binder.getBindingResult().getModel());
|
mavContainer.addAllAttributes(binder.getBindingResult().getModel());
|
||||||
|
|
||||||
|
|
@ -109,23 +119,22 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a {@link WebDataBinder} for a target object.
|
* Creates an instance of the specified model attribute. This method is invoked only if the attribute is
|
||||||
|
* not available in the model. This default implementation uses the no-argument constructor.
|
||||||
|
* Subclasses can override to provide additional means of creating the model attribute.
|
||||||
|
*
|
||||||
|
* @param attributeName the name of the model attribute
|
||||||
|
* @param parameter the method argument declaring the model attribute
|
||||||
|
* @param binderFactory a factory for creating {@link WebDataBinder} instances
|
||||||
|
* @param request the current request
|
||||||
|
* @return the created model attribute; never {@code null}
|
||||||
|
* @throws Exception raised in the process of creating the instance
|
||||||
*/
|
*/
|
||||||
private WebDataBinder createDataBinder(MethodParameter parameter,
|
protected Object createAttribute(String attributeName,
|
||||||
ModelAndViewContainer mavContainer,
|
MethodParameter parameter,
|
||||||
NativeWebRequest webRequest,
|
WebDataBinderFactory binderFactory,
|
||||||
WebDataBinderFactory binderFactory) throws Exception {
|
NativeWebRequest request) throws Exception {
|
||||||
String attrName = ModelFactory.getNameForParameter(parameter);
|
return BeanUtils.instantiateClass(parameter.getParameterType());
|
||||||
|
|
||||||
Object target;
|
|
||||||
if (mavContainer.containsAttribute(attrName)) {
|
|
||||||
target = mavContainer.getAttribute(attrName);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
target = BeanUtils.instantiateClass(parameter.getParameterType());
|
|
||||||
}
|
|
||||||
|
|
||||||
return binderFactory.createBinder(webRequest, target, attrName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -134,17 +143,17 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol
|
||||||
* @param binder the binder with the target object to apply request values to
|
* @param binder the binder with the target object to apply request values to
|
||||||
* @param request the current request
|
* @param request the current request
|
||||||
*/
|
*/
|
||||||
protected void doBind(WebDataBinder binder, NativeWebRequest request) {
|
protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
|
||||||
((WebRequestDataBinder) binder).bind(request);
|
((WebRequestDataBinder) binder).bind(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether to validate the target object of the given {@link WebDataBinder} instance.
|
* Whether to validate the model attribute inside the given data binder instance.
|
||||||
* @param binder the data binder containing the validation candidate
|
* @param binder the data binder containing the validation candidate
|
||||||
* @param parameter the method argument for which data binding is performed
|
* @param parameter the method argument declaring the validation candidate
|
||||||
* @return true if {@link DataBinder#validate()} should be invoked, false otherwise.
|
* @return {@code true} if validation should be applied, {@code false} otherwise.
|
||||||
*/
|
*/
|
||||||
protected boolean shouldValidate(WebDataBinder binder, MethodParameter parameter) {
|
protected boolean isValidationApplicable(WebDataBinder binder, MethodParameter parameter) {
|
||||||
Annotation[] annotations = parameter.getParameterAnnotations();
|
Annotation[] annotations = parameter.getParameterAnnotations();
|
||||||
for (Annotation annot : annotations) {
|
for (Annotation annot : annotations) {
|
||||||
if ("Valid".equals(annot.annotationType().getSimpleName())) {
|
if ("Valid".equals(annot.annotationType().getSimpleName())) {
|
||||||
|
|
@ -160,7 +169,7 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol
|
||||||
* @param parameter the method argument for which data binding is performed
|
* @param parameter the method argument for which data binding is performed
|
||||||
* @return true if the binding or validation errors should result in a {@link BindException}, false otherwise.
|
* @return true if the binding or validation errors should result in a {@link BindException}, false otherwise.
|
||||||
*/
|
*/
|
||||||
protected boolean failOnError(WebDataBinder binder, MethodParameter parameter) {
|
protected boolean isBindExceptionRequired(WebDataBinder binder, MethodParameter parameter) {
|
||||||
int i = parameter.getParameterIndex();
|
int i = parameter.getParameterIndex();
|
||||||
Class<?>[] paramTypes = parameter.getMethod().getParameterTypes();
|
Class<?>[] paramTypes = parameter.getMethod().getParameterTypes();
|
||||||
boolean hasBindingResult = (paramTypes.length > (i + 1) && Errors.class.isAssignableFrom(paramTypes[i + 1]));
|
boolean hasBindingResult = (paramTypes.length > (i + 1) && Errors.class.isAssignableFrom(paramTypes[i + 1]));
|
||||||
|
|
|
||||||
|
|
@ -83,7 +83,7 @@ public class ModelAttributeMethodProcessorTests {
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
processor = new ModelAttributeMethodProcessor(true);
|
processor = new ModelAttributeMethodProcessor(false);
|
||||||
|
|
||||||
Method method = ModelAttributeHandler.class.getDeclaredMethod("modelAttribute",
|
Method method = ModelAttributeHandler.class.getDeclaredMethod("modelAttribute",
|
||||||
TestBean.class, Errors.class, int.class, TestBean.class, TestBean.class);
|
TestBean.class, Errors.class, int.class, TestBean.class, TestBean.class);
|
||||||
|
|
@ -103,68 +103,86 @@ public class ModelAttributeMethodProcessorTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void supportParameter() throws Exception {
|
public void supportedParameters() throws Exception {
|
||||||
processor = new ModelAttributeMethodProcessor(true);
|
// Only @ModelAttribute arguments
|
||||||
assertTrue(processor.supportsParameter(paramNamedValidModelAttr));
|
assertTrue(processor.supportsParameter(paramNamedValidModelAttr));
|
||||||
assertTrue(processor.supportsParameter(paramErrors));
|
|
||||||
assertFalse(processor.supportsParameter(paramInt));
|
|
||||||
assertTrue(processor.supportsParameter(paramModelAttr));
|
assertTrue(processor.supportsParameter(paramModelAttr));
|
||||||
assertTrue(processor.supportsParameter(paramNonSimpleType));
|
|
||||||
|
|
||||||
processor = new ModelAttributeMethodProcessor(false);
|
|
||||||
assertTrue(processor.supportsParameter(paramNamedValidModelAttr));
|
|
||||||
assertFalse(processor.supportsParameter(paramErrors));
|
assertFalse(processor.supportsParameter(paramErrors));
|
||||||
assertFalse(processor.supportsParameter(paramInt));
|
assertFalse(processor.supportsParameter(paramInt));
|
||||||
assertTrue(processor.supportsParameter(paramModelAttr));
|
|
||||||
assertFalse(processor.supportsParameter(paramNonSimpleType));
|
assertFalse(processor.supportsParameter(paramNonSimpleType));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void supportsReturnType() throws Exception {
|
public void supportedParametersInDefaultResolutionMode() throws Exception {
|
||||||
processor = new ModelAttributeMethodProcessor(true);
|
processor = new ModelAttributeMethodProcessor(true);
|
||||||
assertTrue(processor.supportsReturnType(returnParamNamedModelAttr));
|
|
||||||
assertFalse(processor.supportsReturnType(returnParamNonSimpleType));
|
|
||||||
|
|
||||||
|
// Only non-simple types, even if not annotated
|
||||||
|
assertTrue(processor.supportsParameter(paramNamedValidModelAttr));
|
||||||
|
assertTrue(processor.supportsParameter(paramErrors));
|
||||||
|
assertTrue(processor.supportsParameter(paramModelAttr));
|
||||||
|
assertTrue(processor.supportsParameter(paramNonSimpleType));
|
||||||
|
|
||||||
|
assertFalse(processor.supportsParameter(paramInt));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void supportedReturnTypes() throws Exception {
|
||||||
processor = new ModelAttributeMethodProcessor(false);
|
processor = new ModelAttributeMethodProcessor(false);
|
||||||
assertTrue(processor.supportsReturnType(returnParamNamedModelAttr));
|
assertTrue(processor.supportsReturnType(returnParamNamedModelAttr));
|
||||||
assertFalse(processor.supportsReturnType(returnParamNonSimpleType));
|
assertFalse(processor.supportsReturnType(returnParamNonSimpleType));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldValidate() throws Exception {
|
public void supportedReturnTypesInDefaultResolutionMode() throws Exception {
|
||||||
assertTrue(processor.shouldValidate(null, paramNamedValidModelAttr));
|
processor = new ModelAttributeMethodProcessor(true);
|
||||||
assertFalse(processor.shouldValidate(null, paramNonSimpleType));
|
assertTrue(processor.supportsReturnType(returnParamNamedModelAttr));
|
||||||
|
assertFalse(processor.supportsReturnType(returnParamNonSimpleType));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void failOnError() throws Exception {
|
public void validationApplicable() throws Exception {
|
||||||
assertFalse("Shouldn't failOnError with BindingResult", processor.failOnError(null, paramNamedValidModelAttr));
|
assertTrue(processor.isValidationApplicable(null, paramNamedValidModelAttr));
|
||||||
assertTrue("Should failOnError without BindingResult", processor.failOnError(null, paramNonSimpleType));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void createBinderFromModelAttribute() throws Exception {
|
public void validationNotApplicable() throws Exception {
|
||||||
createBinderFromModelAttr("attrName", paramNamedValidModelAttr);
|
assertFalse(processor.isValidationApplicable(null, paramNonSimpleType));
|
||||||
createBinderFromModelAttr("testBean", paramModelAttr);
|
|
||||||
createBinderFromModelAttr("testBean", paramNonSimpleType);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createBinderFromModelAttr(String expectedAttrName, MethodParameter param) throws Exception {
|
@Test
|
||||||
|
public void bindExceptionRequired() throws Exception {
|
||||||
|
assertTrue(processor.isBindExceptionRequired(null, paramNonSimpleType));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void bindExceptionNotRequired() throws Exception {
|
||||||
|
assertFalse(processor.isBindExceptionRequired(null, paramNamedValidModelAttr));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getAttributeFromModel() throws Exception {
|
||||||
|
testGetAttributeFromModel("attrName", paramNamedValidModelAttr);
|
||||||
|
testGetAttributeFromModel("testBean", paramModelAttr);
|
||||||
|
testGetAttributeFromModel("testBean", paramNonSimpleType);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void testGetAttributeFromModel(String expectedAttributeName, MethodParameter param) throws Exception {
|
||||||
Object target = new TestBean();
|
Object target = new TestBean();
|
||||||
mavContainer.addAttribute(expectedAttrName, target);
|
mavContainer.addAttribute(expectedAttributeName, target);
|
||||||
|
|
||||||
WebDataBinder dataBinder = new WebRequestDataBinder(target);
|
WebDataBinder dataBinder = new WebRequestDataBinder(target);
|
||||||
WebDataBinderFactory binderFactory = createMock(WebDataBinderFactory.class);
|
WebDataBinderFactory factory = createMock(WebDataBinderFactory.class);
|
||||||
expect(binderFactory.createBinder(webRequest, target, expectedAttrName)).andReturn(dataBinder);
|
expect(factory.createBinder(webRequest, target, expectedAttributeName)).andReturn(dataBinder);
|
||||||
replay(binderFactory);
|
replay(factory);
|
||||||
|
|
||||||
processor.resolveArgument(param, mavContainer, webRequest, binderFactory);
|
processor.resolveArgument(param, mavContainer, webRequest, factory);
|
||||||
|
|
||||||
verify(binderFactory);
|
verify(factory);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void createBinderWithAttributeConstructor() throws Exception {
|
public void createAttribute() throws Exception {
|
||||||
WebDataBinder dataBinder = new WebRequestDataBinder(null);
|
WebDataBinder dataBinder = new WebRequestDataBinder(null);
|
||||||
|
|
||||||
WebDataBinderFactory factory = createMock(WebDataBinderFactory.class);
|
WebDataBinderFactory factory = createMock(WebDataBinderFactory.class);
|
||||||
|
|
@ -177,7 +195,7 @@ public class ModelAttributeMethodProcessorTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void bindAndValidate() throws Exception {
|
public void automaticValidation() throws Exception {
|
||||||
Object target = new TestBean();
|
Object target = new TestBean();
|
||||||
mavContainer.addAttribute("attrName", target);
|
mavContainer.addAttribute("attrName", target);
|
||||||
|
|
||||||
|
|
@ -193,7 +211,7 @@ public class ModelAttributeMethodProcessorTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=BindException.class)
|
@Test(expected=BindException.class)
|
||||||
public void bindAndFail() throws Exception {
|
public void bindException() throws Exception {
|
||||||
Object target = new TestBean();
|
Object target = new TestBean();
|
||||||
mavContainer.getModel().addAttribute(target);
|
mavContainer.getModel().addAttribute(target);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue