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