/** * Create the target URL and, if necessary, pre-pend the contextPath, expand * URI template variables, append the current request query, and apply the * configured {@link #getRequestDataValueProcessor() * RequestDataValueProcessor}. */ protected final String createTargetUrl(Map<String, Object> model, ServerWebExchange exchange) { String url = getUrl(); Assert.state(url != null, "'url' not set"); ServerHttpRequest request = exchange.getRequest(); StringBuilder targetUrl = new StringBuilder(); if (isContextRelative() && url.startsWith("/")) { targetUrl.append(request.getPath().contextPath().value()); } targetUrl.append(url); if (StringUtils.hasText(targetUrl)) { Map<String, String> uriVars = getCurrentUriVariables(exchange); targetUrl = expandTargetUrlTemplate(targetUrl.toString(), model, uriVars); } if (isPropagateQuery()) { targetUrl = appendCurrentRequestQuery(targetUrl.toString(), request); } String result = targetUrl.toString(); RequestDataValueProcessor processor = getRequestDataValueProcessor(); return (processor != null ? processor.processUrl(exchange, result) : result); }
@Test public void redirectWithAbsoluteUrl() throws Exception { Rendering rendering = Rendering.redirectTo("foo").contextRelative(false).build(); Object view = rendering.view(); assertEquals(RedirectView.class, view.getClass()); assertFalse(((RedirectView) view).isContextRelative()); }
@Test public void defaultValuesForRedirect() throws Exception { Rendering rendering = Rendering.redirectTo("abc").build(); Object view = rendering.view(); assertEquals(RedirectView.class, view.getClass()); assertEquals("abc", ((RedirectView) view).getUrl()); assertTrue(((RedirectView) view).isContextRelative()); assertFalse(((RedirectView) view).isPropagateQuery()); }
/** * Create the target URL and, if necessary, pre-pend the contextPath, expand * URI template variables, append the current request query, and apply the * configured {@link #getRequestDataValueProcessor() * RequestDataValueProcessor}. */ protected final String createTargetUrl(Map<String, Object> model, ServerWebExchange exchange) { StringBuilder targetUrl = new StringBuilder(); if (isContextRelative() && getUrl().startsWith("/")) { targetUrl.append(exchange.getRequest().getContextPath()); } targetUrl.append(getUrl()); if (StringUtils.hasText(targetUrl)) { Map<String, String> uriVars = getCurrentUriVariables(exchange); targetUrl = expandTargetUrlTemplate(targetUrl.toString(), model, uriVars); } if (isPropagateQuery()) { targetUrl = appendCurrentRequestQuery(targetUrl.toString(), exchange.getRequest()); } String result = targetUrl.toString(); RequestDataValueProcessor processor = getRequestDataValueProcessor(); return (processor != null ? processor.processUrl(exchange, result) : result); }