optional boolean parameters in MVC handler methods resolve to "false" if not present

This commit is contained in:
Juergen Hoeller
2009-02-13 11:46:55 +00:00
parent 721f2ca63e
commit 6b2d4a81a0
2 changed files with 39 additions and 11 deletions

View File

@@ -384,7 +384,7 @@ public class HandlerMethodInvoker {
else if (required) {
raiseMissingParameterException(paramName, paramType);
}
checkValue(paramName, paramValue, paramType);
paramValue = checkValue(paramName, paramValue, paramType);
}
WebDataBinder binder = createBinder(webRequest, null, paramName);
initBinder(handlerForInitBinderCall, paramName, binder, webRequest);
@@ -411,7 +411,7 @@ public class HandlerMethodInvoker {
else if (required) {
raiseMissingHeaderException(headerName, paramType);
}
checkValue(headerName, headerValue, paramType);
headerValue = checkValue(headerName, headerValue, paramType);
}
WebDataBinder binder = createBinder(webRequest, null, headerName);
initBinder(handlerForInitBinderCall, headerName, binder, webRequest);
@@ -434,7 +434,7 @@ public class HandlerMethodInvoker {
else if (required) {
raiseMissingCookieException(cookieName, paramType);
}
checkValue(cookieName, cookieValue, paramType);
cookieValue = checkValue(cookieName, cookieValue, paramType);
}
WebDataBinder binder = createBinder(webRequest, null, cookieName);
initBinder(handlerForInitBinderCall, cookieName, binder, webRequest);
@@ -484,12 +484,18 @@ public class HandlerMethodInvoker {
return name;
}
private void checkValue(String name, Object value, Class paramType) {
if (value == null && paramType.isPrimitive()) {
throw new IllegalStateException("Optional " + paramType + " parameter '" + name +
"' is not present but cannot be translated into a null value due to being declared as a " +
"primitive type. Consider declaring it as object wrapper for the corresponding primitive type.");
private Object checkValue(String name, Object value, Class paramType) {
if (value == null) {
if (boolean.class.equals(paramType)) {
return Boolean.FALSE;
}
else if (paramType.isPrimitive()) {
throw new IllegalStateException("Optional " + paramType + " parameter '" + name +
"' is not present but cannot be translated into a null value due to being declared as a " +
"primitive type. Consider declaring it as object wrapper for the corresponding primitive type.");
}
}
return value;
}
private WebDataBinder resolveModelAttribute(String attrName, MethodParameter methodParam,