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.
master
Rossen Stoyanchev 13 years ago
parent 5afe139285
commit 57fffb147c
  1. 14
      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 * A Servlet-specific {@link ModelAttributeMethodProcessor} that applies data
* binding through a WebDataBinder of type {@link ServletRequestDataBinder}. * binding through a WebDataBinder of type {@link ServletRequestDataBinder}.
* *
* <p>Also adds a fall-back strategy to instantiate a model attribute from a * <p>Adds a fall-back strategy to instantiate a model attribute from a
* URI template variable combined with type conversion, if the model attribute * URI template variable combined with type conversion, if the model attribute
* name matches to a URI template variable name. * name matches to a URI template variable name.
* *
@ -57,9 +57,8 @@ public class ServletModelAttributeMethodProcessor extends ModelAttributeMethodPr
/** /**
* Add a fall-back strategy to instantiate the model attribute from a URI * Add a fall-back strategy to instantiate the model attribute from a URI
* template variable and type conversion, assuming the model attribute * template variable with type conversion, if the model attribute name
* name matches to a URI variable name. If instantiation fails for _any_ * matches to a URI variable name.
* reason, the call is delegated to the base class.
*/ */
@Override @Override
protected Object createAttribute(String attributeName, protected Object createAttribute(String attributeName,
@ -70,15 +69,8 @@ public class ServletModelAttributeMethodProcessor extends ModelAttributeMethodPr
Map<String, String> uriVariables = getUriTemplateVariables(request); Map<String, String> uriVariables = getUriTemplateVariables(request);
if (uriVariables.containsKey(attributeName)) { if (uriVariables.containsKey(attributeName)) {
try {
DataBinder binder = binderFactory.createBinder(request, null, attributeName); DataBinder binder = binderFactory.createBinder(request, null, attributeName);
return binder.convertIfNecessary(uriVariables.get(attributeName), parameter.getParameterType()); 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());
}
} }
return super.createAttribute(attributeName, parameter, binderFactory, request); return super.createAttribute(attributeName, parameter, binderFactory, request);

Loading…
Cancel
Save