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,

View File

@ -130,6 +130,28 @@ public class ServletAnnotationControllerTests {
servlet.service(request, response);
}
@Test
public void optionalParamPresent() throws Exception {
@SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() {
@Override
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
GenericWebApplicationContext wac = new GenericWebApplicationContext();
wac.registerBeanDefinition("controller", new RootBeanDefinition(OptionalParamController.class));
wac.refresh();
return wac;
}
};
servlet.init(new MockServletConfig());
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
request.addParameter("id", "val");
request.addParameter("flag", "true");
request.addHeader("header", "otherVal");
MockHttpServletResponse response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals("val-true-otherVal", response.getContentAsString());
}
@Test
public void optionalParamMissing() throws Exception {
@SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() {
@ -146,7 +168,7 @@ public class ServletAnnotationControllerTests {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
MockHttpServletResponse response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals("null-null", response.getContentAsString());
assertEquals("null-false-null", response.getContentAsString());
}
@Test
@ -1318,10 +1340,10 @@ public class ServletAnnotationControllerTests {
public static class OptionalParamController {
@RequestMapping("/myPath.do")
public void myHandle(@RequestParam(required = false) String id,
public void myHandle(@RequestParam(required = false) String id, @RequestParam(required = false) boolean flag,
@RequestHeader(value = "header", required = false) String header, HttpServletResponse response)
throws IOException {
response.getWriter().write(String.valueOf(id) + "-" + String.valueOf(header));
response.getWriter().write(String.valueOf(id) + "-" + flag + "-" + String.valueOf(header));
}
}