DefaultRequestToViewNameTranslator strips trailing slashes as well (SPR-6830)
This commit is contained in:
parent
fff4c77dbe
commit
103ac0359d
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* Copyright 2002-2010 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.
|
||||
|
|
@ -66,6 +66,8 @@ public class DefaultRequestToViewNameTranslator implements RequestToViewNameTran
|
|||
|
||||
private boolean stripLeadingSlash = true;
|
||||
|
||||
private boolean stripTrailingSlash = true;
|
||||
|
||||
private boolean stripExtension = true;
|
||||
|
||||
private UrlPathHelper urlPathHelper = new UrlPathHelper();
|
||||
|
|
@ -91,7 +93,6 @@ public class DefaultRequestToViewNameTranslator implements RequestToViewNameTran
|
|||
* Set the value that will replace '<code>/</code>' as the separator
|
||||
* in the view name. The default behavior simply leaves '<code>/</code>'
|
||||
* as the separator.
|
||||
* @param separator the desired separator value
|
||||
*/
|
||||
public void setSeparator(String separator) {
|
||||
this.separator = separator;
|
||||
|
|
@ -100,16 +101,22 @@ public class DefaultRequestToViewNameTranslator implements RequestToViewNameTran
|
|||
/**
|
||||
* Set whether or not leading slashes should be stripped from the URI when
|
||||
* generating the view name. Default is "true".
|
||||
* @param stripLeadingSlash <code>true</code> if leading slashes are to be stripped
|
||||
*/
|
||||
public void setStripLeadingSlash(boolean stripLeadingSlash) {
|
||||
this.stripLeadingSlash = stripLeadingSlash;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether or not trailing slashes should be stripped from the URI when
|
||||
* generating the view name. Default is "true".
|
||||
*/
|
||||
public void setStripTrailingSlash(boolean stripTrailingSlash) {
|
||||
this.stripTrailingSlash = stripTrailingSlash;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether or not file extensions should be stripped from the URI when
|
||||
* generating the view name. Default is "true".
|
||||
* @param stripExtension <code>true</code> if file extensions should be stripped
|
||||
*/
|
||||
public void setStripExtension(boolean stripExtension) {
|
||||
this.stripExtension = stripExtension;
|
||||
|
|
@ -120,7 +127,6 @@ public class DefaultRequestToViewNameTranslator implements RequestToViewNameTran
|
|||
* context. Else, the path within the current servlet mapping is used
|
||||
* if applicable (i.e. in the case of a ".../*" servlet mapping in web.xml).
|
||||
* Default is "false".
|
||||
* @param alwaysUseFullPath <code>true</code> if URL lookup should always use the full path
|
||||
* @see org.springframework.web.util.UrlPathHelper#setAlwaysUseFullPath
|
||||
*/
|
||||
public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
|
||||
|
|
@ -144,8 +150,6 @@ public class DefaultRequestToViewNameTranslator implements RequestToViewNameTran
|
|||
* the resolution of lookup paths.
|
||||
* <p>Use this to override the default UrlPathHelper with a custom subclass,
|
||||
* or to share common UrlPathHelper settings across multiple web components.
|
||||
* @param urlPathHelper the desired helper
|
||||
* @throws IllegalArgumentException if the supplied UrlPathHelper is <code>null</code>
|
||||
*/
|
||||
public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
|
||||
Assert.notNull(urlPathHelper, "UrlPathHelper must not be null");
|
||||
|
|
@ -177,6 +181,9 @@ public class DefaultRequestToViewNameTranslator implements RequestToViewNameTran
|
|||
if (this.stripLeadingSlash && path.startsWith(SLASH)) {
|
||||
path = path.substring(1);
|
||||
}
|
||||
if (this.stripTrailingSlash && path.endsWith(SLASH)) {
|
||||
path = path.substring(0, path.length() - 1);
|
||||
}
|
||||
if (this.stripExtension) {
|
||||
path = StringUtils.stripFilenameExtension(path);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2006 the original author or authors.
|
||||
* Copyright 2002-2010 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.
|
||||
|
|
@ -16,15 +16,17 @@
|
|||
|
||||
package org.springframework.web.servlet.view;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
|
||||
/**
|
||||
* Unit tests for the DefaultRequestToViewNameTranslator class.
|
||||
*
|
||||
* @author Rick Evans
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public final class DefaultRequestToViewNameTranslatorTests extends TestCase {
|
||||
public final class DefaultRequestToViewNameTranslatorTests {
|
||||
|
||||
private static final String VIEW_NAME = "apple";
|
||||
private static final String EXTENSION = ".html";
|
||||
|
|
@ -34,68 +36,86 @@ public final class DefaultRequestToViewNameTranslatorTests extends TestCase {
|
|||
private MockHttpServletRequest request;
|
||||
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.translator = new DefaultRequestToViewNameTranslator();
|
||||
this.request = new MockHttpServletRequest();
|
||||
this.request.setContextPath(CONTEXT_PATH);
|
||||
}
|
||||
|
||||
|
||||
public void TODO_testGetViewNameLeavesLeadingSlashIfSoConfigured() throws Exception {
|
||||
request.setRequestURI(CONTEXT_PATH + VIEW_NAME);
|
||||
@Test
|
||||
public void testGetViewNameLeavesLeadingSlashIfSoConfigured() {
|
||||
request.setRequestURI(CONTEXT_PATH + "/" + VIEW_NAME + "/");
|
||||
this.translator.setStripLeadingSlash(false);
|
||||
assertViewName("/" + VIEW_NAME);
|
||||
}
|
||||
|
||||
public void testGetViewNameLeavesExtensionIfSoConfigured() throws Exception {
|
||||
request.setRequestURI(CONTEXT_PATH + VIEW_NAME + EXTENSION);
|
||||
@Test
|
||||
public void testGetViewNameLeavesTrailingSlashIfSoConfigured() {
|
||||
request.setRequestURI(CONTEXT_PATH + "/" + VIEW_NAME + "/");
|
||||
this.translator.setStripTrailingSlash(false);
|
||||
assertViewName(VIEW_NAME + "/");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetViewNameLeavesExtensionIfSoConfigured() {
|
||||
request.setRequestURI(CONTEXT_PATH + "/" + VIEW_NAME + EXTENSION);
|
||||
this.translator.setStripExtension(false);
|
||||
assertViewName(VIEW_NAME + EXTENSION);
|
||||
}
|
||||
|
||||
public void testGetViewNameWithDefaultConfiguration() throws Exception {
|
||||
@Test
|
||||
public void testGetViewNameWithDefaultConfiguration() {
|
||||
request.setRequestURI(CONTEXT_PATH + VIEW_NAME + EXTENSION);
|
||||
assertViewName(VIEW_NAME);
|
||||
}
|
||||
|
||||
public void testGetViewNameWithCustomSeparator() throws Exception {
|
||||
@Test
|
||||
public void testGetViewNameWithCustomSeparator() {
|
||||
request.setRequestURI(CONTEXT_PATH + VIEW_NAME + "/fiona" + EXTENSION);
|
||||
this.translator.setSeparator("_");
|
||||
assertViewName(VIEW_NAME + "_fiona");
|
||||
}
|
||||
|
||||
public void testGetViewNameWithNoExtension() throws Exception {
|
||||
@Test
|
||||
public void testGetViewNameWithNoExtension() {
|
||||
request.setRequestURI(CONTEXT_PATH + VIEW_NAME);
|
||||
assertViewName(VIEW_NAME);
|
||||
}
|
||||
|
||||
public void testGetViewNameWithPrefix() throws Exception {
|
||||
@Test
|
||||
public void testGetViewNameWithPrefix() {
|
||||
final String prefix = "fiona_";
|
||||
request.setRequestURI(CONTEXT_PATH + VIEW_NAME);
|
||||
this.translator.setPrefix(prefix);
|
||||
assertViewName(prefix + VIEW_NAME);
|
||||
}
|
||||
|
||||
public void testGetViewNameWithNullPrefix() throws Exception {
|
||||
@Test
|
||||
public void testGetViewNameWithNullPrefix() {
|
||||
request.setRequestURI(CONTEXT_PATH + VIEW_NAME);
|
||||
this.translator.setPrefix(null);
|
||||
assertViewName(VIEW_NAME);
|
||||
}
|
||||
|
||||
public void testGetViewNameWithSuffix() throws Exception {
|
||||
@Test
|
||||
public void testGetViewNameWithSuffix() {
|
||||
final String suffix = ".fiona";
|
||||
request.setRequestURI(CONTEXT_PATH + VIEW_NAME);
|
||||
this.translator.setSuffix(suffix);
|
||||
assertViewName(VIEW_NAME + suffix);
|
||||
}
|
||||
|
||||
public void testGetViewNameWithNullSuffix() throws Exception {
|
||||
@Test
|
||||
public void testGetViewNameWithNullSuffix() {
|
||||
request.setRequestURI(CONTEXT_PATH + VIEW_NAME);
|
||||
this.translator.setSuffix(null);
|
||||
assertViewName(VIEW_NAME);
|
||||
}
|
||||
|
||||
public void testTrySetUrlPathHelperToNull() throws Exception {
|
||||
@Test
|
||||
public void testTrySetUrlPathHelperToNull() {
|
||||
try {
|
||||
this.translator.setUrlPathHelper(null);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue