@RequestMapping type-level param constraints taken into account consistently
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user