From 57fffb147c1ae38a582ade31ed79dfa06ee32141 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 27 Sep 2011 11:22:43 +0000 Subject: [PATCH] SPR-7608 ALLOW EXCEPTION WHEN MODEL ATTR IS CREATED FROM URI VARIABLE When a @ModelAttribute is instantiated from a URI variable with type conversion, if conversion fails allow the exception to propagate. This is consistent with what happens on type conversion failure with @PathVariable and other args that rely on type conversion. --- .../ServletModelAttributeMethodProcessor.java | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/ServletModelAttributeMethodProcessor.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/ServletModelAttributeMethodProcessor.java index ff02806524..b435d5f2ca 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/ServletModelAttributeMethodProcessor.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/ServletModelAttributeMethodProcessor.java @@ -36,7 +36,7 @@ import org.springframework.web.servlet.mvc.method.annotation.ServletRequestDataB * A Servlet-specific {@link ModelAttributeMethodProcessor} that applies data * binding through a WebDataBinder of type {@link ServletRequestDataBinder}. * - *

Also adds a fall-back strategy to instantiate a model attribute from a + *

Adds a fall-back strategy to instantiate a model attribute from a * URI template variable combined with type conversion, if the model attribute * name matches to a URI template variable name. * @@ -57,28 +57,20 @@ public class ServletModelAttributeMethodProcessor extends ModelAttributeMethodPr /** * Add a fall-back strategy to instantiate the model attribute from a URI - * template variable and type conversion, assuming the model attribute - * name matches to a URI variable name. If instantiation fails for _any_ - * reason, the call is delegated to the base class. + * template variable with type conversion, if the model attribute name + * matches to a URI variable name. */ @Override protected Object createAttribute(String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception { - + Map uriVariables = getUriTemplateVariables(request); - + if (uriVariables.containsKey(attributeName)) { - try { - DataBinder binder = binderFactory.createBinder(request, null, attributeName); - return binder.convertIfNecessary(uriVariables.get(attributeName), parameter.getParameterType()); - - } catch (Exception exception) { - logger.info("Model attribute name '" + attributeName + "' matches to a URI template variable name " - + "but the variable String value could not be converted into an attribute instance: " - + exception.getMessage()); - } + DataBinder binder = binderFactory.createBinder(request, null, attributeName); + return binder.convertIfNecessary(uriVariables.get(attributeName), parameter.getParameterType()); } return super.createAttribute(attributeName, parameter, binderFactory, request); @@ -90,7 +82,7 @@ public class ServletModelAttributeMethodProcessor extends ModelAttributeMethodPr Map uriTemplateVars = (Map) request.getAttribute( HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST); - + return (uriTemplateVars != null) ? uriTemplateVars : Collections.emptyMap(); }