From 36970115ed2e6e0104d0458a035e8c714ede3e95 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Sun, 10 Apr 2011 06:01:11 +0000 Subject: [PATCH] Fix issues encountered in Greenhouse --- .../support/DefaultMethodReturnValueHandler.java | 8 +++++--- .../ServletRequestMethodArgumentResolver.java | 7 ++++++- .../DefaultMethodReturnValueHandlerTests.java | 12 ++++++++++++ ...ervletRequestMethodArgumentResolverTests.java | 16 ++++++++++++++-- 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/DefaultMethodReturnValueHandler.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/DefaultMethodReturnValueHandler.java index 84d5cb904f..01fd0b4848 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/DefaultMethodReturnValueHandler.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/DefaultMethodReturnValueHandler.java @@ -79,10 +79,12 @@ public class DefaultMethodReturnValueHandler implements HandlerMethodReturnValue else if (!BeanUtils.isSimpleProperty(returnValue.getClass())) { String name = ModelFactory.getNameForReturnValue(returnValue, returnType); mavContainer.addAttribute(name, returnValue); + return; + } + else { + // should not happen + throw new UnsupportedOperationException(); } - - // should not happen - throw new UnsupportedOperationException(); } } diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/ServletRequestMethodArgumentResolver.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/ServletRequestMethodArgumentResolver.java index 6a59ce2d27..27b8b57b17 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/ServletRequestMethodArgumentResolver.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/ServletRequestMethodArgumentResolver.java @@ -29,6 +29,7 @@ import javax.servlet.http.HttpSession; import org.springframework.core.MethodParameter; import org.springframework.web.bind.support.WebDataBinderFactory; import org.springframework.web.context.request.NativeWebRequest; +import org.springframework.web.context.request.WebRequest; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.ModelAndViewContainer; import org.springframework.web.multipart.MultipartRequest; @@ -43,7 +44,8 @@ public class ServletRequestMethodArgumentResolver implements HandlerMethodArgume public boolean supportsParameter(MethodParameter parameter) { Class parameterType = parameter.getParameterType(); - return ServletRequest.class.isAssignableFrom(parameterType) || + return WebRequest.class.isAssignableFrom(parameterType) || + ServletRequest.class.isAssignableFrom(parameterType) || MultipartRequest.class.isAssignableFrom(parameterType) || HttpSession.class.isAssignableFrom(parameterType) || Principal.class.isAssignableFrom(parameterType) || Locale.class.equals(parameterType) || InputStream.class.isAssignableFrom(parameterType) || @@ -61,6 +63,9 @@ public class ServletRequestMethodArgumentResolver implements HandlerMethodArgume HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class); Class parameterType = parameter.getParameterType(); + if (WebRequest.class.isAssignableFrom(parameterType)) { + return webRequest; + } if (ServletRequest.class.isAssignableFrom(parameterType) || MultipartRequest.class.isAssignableFrom(parameterType)) { Object nativeRequest = webRequest.getNativeRequest(parameterType); diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/DefaultMethodReturnValueHandlerTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/DefaultMethodReturnValueHandlerTests.java index b1e21e1b08..5237bf88f5 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/DefaultMethodReturnValueHandlerTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/DefaultMethodReturnValueHandlerTests.java @@ -23,6 +23,7 @@ import java.lang.reflect.Method; 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.ui.ExtendedModelMap; @@ -61,6 +62,12 @@ public class DefaultMethodReturnValueHandlerTests { assertNull(mavContainer.getViewName()); assertTrue(mavContainer.getModel().isEmpty()); } + + @Test + public void returnSingleModelAttribute() throws Exception{ + handler.handleReturnValue(new TestBean(), createMethodParam("singleModelAttribute"), mavContainer, webRequest); + assertTrue(mavContainer.containsAttribute("testBean")); + } private MethodParameter createMethodParam(String methodName) throws Exception { Method method = getClass().getDeclaredMethod(methodName); @@ -76,4 +83,9 @@ public class DefaultMethodReturnValueHandlerTests { private void voidReturnValue() { } + @SuppressWarnings("unused") + private TestBean singleModelAttribute() { + return null; + } + } diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/ServletRequestMethodArgumentResolverTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/ServletRequestMethodArgumentResolverTests.java index 0360a5c922..5748cf7a6b 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/ServletRequestMethodArgumentResolverTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/ServletRequestMethodArgumentResolverTests.java @@ -32,6 +32,7 @@ import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockHttpSession; import org.springframework.web.context.request.ServletWebRequest; +import org.springframework.web.context.request.WebRequest; import org.springframework.web.multipart.MultipartRequest; import org.springframework.web.servlet.mvc.method.annotation.support.ServletRequestMethodArgumentResolver; @@ -55,7 +56,7 @@ public class ServletRequestMethodArgumentResolverTests { resolver = new ServletRequestMethodArgumentResolver(); supportedParams = getClass() .getMethod("supportedParams", ServletRequest.class, MultipartRequest.class, HttpSession.class, - Principal.class, Locale.class, InputStream.class, Reader.class); + Principal.class, Locale.class, InputStream.class, Reader.class, WebRequest.class); servletRequest = new MockHttpServletRequest(); webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse()); } @@ -135,13 +136,24 @@ public class ServletRequestMethodArgumentResolverTests { assertSame("Invalid result", webRequest.getRequest().getReader(), result); } + @Test + public void webRequest() throws Exception { + MethodParameter webRequestParameter = new MethodParameter(supportedParams, 7); + + assertTrue("WebRequest not supported", resolver.supportsParameter(webRequestParameter)); + + Object result = resolver.resolveArgument(webRequestParameter, null, webRequest, null); + assertSame("Invalid result", webRequest, result); + } + public void supportedParams(ServletRequest p0, MultipartRequest p1, HttpSession p2, Principal p3, Locale p4, InputStream p5, - Reader p9) { + Reader p6, + WebRequest p7) { } }