diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/ViewControllerBeanDefinitionParser.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/ViewControllerBeanDefinitionParser.java index 51e63db8e3..4e847cfd98 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/ViewControllerBeanDefinitionParser.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/ViewControllerBeanDefinitionParser.java @@ -140,6 +140,8 @@ class ViewControllerBeanDefinitionParser implements BeanDefinitionParser { } if (element.hasAttribute("context-relative")) { redirectView.getPropertyValues().add("contextRelative", element.getAttribute("context-relative")); + } else { + redirectView.getPropertyValues().add("contextRelative", true); } if (element.hasAttribute("keep-query-params")) { redirectView.getPropertyValues().add("propagateQueryParams", element.getAttribute("keep-query-params")); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java index 7fc8ef0f14..a7de588e6c 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java @@ -95,6 +95,7 @@ import org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor; import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter; +import org.springframework.web.servlet.mvc.ParameterizableViewController; import org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter; import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; @@ -648,9 +649,17 @@ public class MvcNamespaceTests { SimpleUrlHandlerMapping hm = this.appContext.getBean(SimpleUrlHandlerMapping.class); assertNotNull(hm); - assertNotNull(hm.getUrlMap().get("/path")); - assertNotNull(hm.getUrlMap().get("/old")); - assertNotNull(hm.getUrlMap().get("/bad")); + ParameterizableViewController viewController = (ParameterizableViewController) hm.getUrlMap().get("/path"); + assertNotNull(viewController); + assertEquals("home", viewController.getViewName()); + + ParameterizableViewController redirectViewController = (ParameterizableViewController) hm.getUrlMap().get("/old"); + assertNotNull(redirectViewController); + assertThat(redirectViewController.getView(), Matchers.instanceOf(RedirectView.class)); + + ParameterizableViewController statusViewController = (ParameterizableViewController) hm.getUrlMap().get("/bad"); + assertNotNull(statusViewController); + assertEquals(404, statusViewController.getStatusCode().value()); BeanNameUrlHandlerMapping beanNameMapping = this.appContext.getBean(BeanNameUrlHandlerMapping.class); assertNotNull(beanNameMapping);