SPR-2692 Add URI template support to RedirectView
This commit is contained in:
parent
4027cbd952
commit
761a836236
|
|
@ -19,20 +19,29 @@ package org.springframework.web.servlet.view;
|
|||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.lang.reflect.Array;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.web.servlet.View;
|
||||
import org.springframework.web.util.UriTemplate;
|
||||
import org.springframework.web.util.UriUtils;
|
||||
import org.springframework.web.util.WebUtils;
|
||||
|
||||
/**
|
||||
|
|
@ -214,7 +223,7 @@ public class RedirectView extends AbstractUrlBasedView {
|
|||
targetUrl.append(request.getContextPath());
|
||||
}
|
||||
targetUrl.append(getUrl());
|
||||
if (this.exposeModelAttributes) {
|
||||
|
||||
String enc = this.encodingScheme;
|
||||
if (enc == null) {
|
||||
enc = request.getCharacterEncoding();
|
||||
|
|
@ -222,12 +231,43 @@ public class RedirectView extends AbstractUrlBasedView {
|
|||
if (enc == null) {
|
||||
enc = WebUtils.DEFAULT_CHARACTER_ENCODING;
|
||||
}
|
||||
|
||||
UriTemplate uriTemplate = createUriTemplate(targetUrl, enc);
|
||||
if (uriTemplate.getVariableNames().size() > 0) {
|
||||
targetUrl = new StringBuilder(uriTemplate.expand(model).toString());
|
||||
model = removeKeys(model, uriTemplate.getVariableNames());
|
||||
}
|
||||
if (this.exposeModelAttributes) {
|
||||
appendQueryProperties(targetUrl, model, enc);
|
||||
}
|
||||
|
||||
sendRedirect(request, response, targetUrl.toString(), this.http10Compatible);
|
||||
}
|
||||
|
||||
private UriTemplate createUriTemplate(StringBuilder targetUrl, final String encoding) {
|
||||
return new UriTemplate(targetUrl.toString()) {
|
||||
@Override
|
||||
protected URI encodeUri(String uri) {
|
||||
try {
|
||||
String encoded = UriUtils.encodeUri(uri, encoding);
|
||||
return new URI(encoded);
|
||||
} catch (UnsupportedEncodingException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
} catch (URISyntaxException ex) {
|
||||
throw new IllegalArgumentException("Could not create URI from [" + uri + "]: " + ex, ex);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static Map<String, Object> removeKeys(Map<String, Object> map, List<String> keysToRemove) {
|
||||
Map<String, Object> result = new HashMap<String, Object>(map);
|
||||
for (String key : keysToRemove) {
|
||||
result.remove(key);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append query properties to the redirect URL.
|
||||
* Stringifies, URL-encodes and formats model attributes as query properties.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
* Copyright 2002-2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.view;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
|
||||
public class RedirectViewUriTemplateTests {
|
||||
|
||||
private MockHttpServletRequest request;
|
||||
|
||||
private MockHttpServletResponse response;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
request = new MockHttpServletRequest();
|
||||
response = new MockHttpServletResponse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pathVar() throws Exception {
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
model.put("foo", "bar");
|
||||
|
||||
String baseUrl = "http://url.somewhere.com";
|
||||
RedirectView redirectView = new RedirectView(baseUrl + "/{foo}");
|
||||
redirectView.renderMergedOutputModel(model, request, response);
|
||||
|
||||
assertEquals(baseUrl + "/bar", response.getRedirectedUrl());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pathVarAndArrayParam() throws Exception {
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
model.put("foo", "bar");
|
||||
model.put("fooArr", new String[] { "baz", "bazz" });
|
||||
|
||||
RedirectView redirectView = new RedirectView("/foo/{foo}");
|
||||
redirectView.renderMergedOutputModel(model, request, response);
|
||||
|
||||
assertEquals("/foo/bar?fooArr=baz&fooArr=bazz", response.getRedirectedUrl());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pathVarWithObjectConversion() throws Exception {
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
model.put("foo", new Long(611));
|
||||
|
||||
RedirectView redirectView = new RedirectView("/foo/{foo}");
|
||||
redirectView.renderMergedOutputModel(model, request, response);
|
||||
|
||||
assertEquals("/foo/611", response.getRedirectedUrl());
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue