@RequestMapping type-level param constraints taken into account consistently

This commit is contained in:
Juergen Hoeller
2009-02-15 21:31:20 +00:00
parent a25e24f37e
commit 90b5c3a8dd
5 changed files with 183 additions and 21 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -584,6 +584,66 @@ public class ServletAnnotationControllerTests {
assertEquals("mySurpriseView", response.getContentAsString());
}
@Test
public void constrainedParameterDispatchingController() throws Exception {
final MockServletContext servletContext = new MockServletContext();
final MockServletConfig servletConfig = new MockServletConfig(servletContext);
@SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() {
@Override
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
GenericWebApplicationContext wac = new GenericWebApplicationContext();
wac.setServletContext(servletContext);
RootBeanDefinition bd = new RootBeanDefinition(MyConstrainedParameterDispatchingController.class);
bd.setScope(WebApplicationContext.SCOPE_REQUEST);
wac.registerBeanDefinition("controller", bd);
wac.refresh();
return wac;
}
};
servlet.init(servletConfig);
MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
request.addParameter("view", "other");
MockHttpServletResponse response = new MockHttpServletResponse();
try {
servlet.service(request, response);
fail("Should have failed because of type-level parameter constraint not met");
}
catch (ServletException ex) {
// expected
ex.printStackTrace();
}
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
request.addParameter("active", "true");
request.addParameter("view", "other");
response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals("myOtherView", response.getContentAsString());
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
request.addParameter("view", "my");
request.addParameter("lang", "de");
response = new MockHttpServletResponse();
try {
servlet.service(request, response);
fail("Should have failed because of type-level parameter constraint not met");
}
catch (ServletException ex) {
// expected
ex.printStackTrace();
}
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
request.addParameter("view", "my");
request.addParameter("lang", "de");
request.addParameter("active", "true");
response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals("myLangView", response.getContentAsString());
}
@Test
public void methodNameDispatchingController() throws Exception {
@SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() {
@@ -1189,6 +1249,22 @@ public class ServletAnnotationControllerTests {
}
@Controller
@RequestMapping(value = "/myPath.do", params = {"active"})
private static class MyConstrainedParameterDispatchingController {
@RequestMapping(params = {"view", "!lang"})
public void myOtherHandle(HttpServletResponse response) throws IOException {
response.getWriter().write("myOtherView");
}
@RequestMapping(method = RequestMethod.GET, params = {"view=my", "lang=de"})
public void myLangHandle(HttpServletResponse response) throws IOException {
response.getWriter().write("myLangView");
}
}
@Controller
@RequestMapping(value = "/*.do", method = RequestMethod.POST, params = "myParam=myValue")
private static class MyPostMethodNameDispatchingController extends MethodNameDispatchingController {