Skip to content

Commit 103ac03

Browse files
committed
DefaultRequestToViewNameTranslator strips trailing slashes as well (SPR-6830)
1 parent fff4c77 commit 103ac03

File tree

2 files changed

+52
-25
lines changed

2 files changed

+52
-25
lines changed

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/DefaultRequestToViewNameTranslator.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2007 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -66,6 +66,8 @@ public class DefaultRequestToViewNameTranslator implements RequestToViewNameTran
6666

6767
private boolean stripLeadingSlash = true;
6868

69+
private boolean stripTrailingSlash = true;
70+
6971
private boolean stripExtension = true;
7072

7173
private UrlPathHelper urlPathHelper = new UrlPathHelper();
@@ -91,7 +93,6 @@ public void setSuffix(String suffix) {
9193
* Set the value that will replace '<code>/</code>' as the separator
9294
* in the view name. The default behavior simply leaves '<code>/</code>'
9395
* as the separator.
94-
* @param separator the desired separator value
9596
*/
9697
public void setSeparator(String separator) {
9798
this.separator = separator;
@@ -100,16 +101,22 @@ public void setSeparator(String separator) {
100101
/**
101102
* Set whether or not leading slashes should be stripped from the URI when
102103
* generating the view name. Default is "true".
103-
* @param stripLeadingSlash <code>true</code> if leading slashes are to be stripped
104104
*/
105105
public void setStripLeadingSlash(boolean stripLeadingSlash) {
106106
this.stripLeadingSlash = stripLeadingSlash;
107107
}
108108

109+
/**
110+
* Set whether or not trailing slashes should be stripped from the URI when
111+
* generating the view name. Default is "true".
112+
*/
113+
public void setStripTrailingSlash(boolean stripTrailingSlash) {
114+
this.stripTrailingSlash = stripTrailingSlash;
115+
}
116+
109117
/**
110118
* Set whether or not file extensions should be stripped from the URI when
111119
* generating the view name. Default is "true".
112-
* @param stripExtension <code>true</code> if file extensions should be stripped
113120
*/
114121
public void setStripExtension(boolean stripExtension) {
115122
this.stripExtension = stripExtension;
@@ -120,7 +127,6 @@ public void setStripExtension(boolean stripExtension) {
120127
* context. Else, the path within the current servlet mapping is used
121128
* if applicable (i.e. in the case of a ".../*" servlet mapping in web.xml).
122129
* Default is "false".
123-
* @param alwaysUseFullPath <code>true</code> if URL lookup should always use the full path
124130
* @see org.springframework.web.util.UrlPathHelper#setAlwaysUseFullPath
125131
*/
126132
public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
@@ -144,8 +150,6 @@ public void setUrlDecode(boolean urlDecode) {
144150
* the resolution of lookup paths.
145151
* <p>Use this to override the default UrlPathHelper with a custom subclass,
146152
* or to share common UrlPathHelper settings across multiple web components.
147-
* @param urlPathHelper the desired helper
148-
* @throws IllegalArgumentException if the supplied UrlPathHelper is <code>null</code>
149153
*/
150154
public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
151155
Assert.notNull(urlPathHelper, "UrlPathHelper must not be null");
@@ -177,6 +181,9 @@ protected String transformPath(String lookupPath) {
177181
if (this.stripLeadingSlash && path.startsWith(SLASH)) {
178182
path = path.substring(1);
179183
}
184+
if (this.stripTrailingSlash && path.endsWith(SLASH)) {
185+
path = path.substring(0, path.length() - 1);
186+
}
180187
if (this.stripExtension) {
181188
path = StringUtils.stripFilenameExtension(path);
182189
}

org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/DefaultRequestToViewNameTranslatorTests.java

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2006 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,15 +16,17 @@
1616

1717
package org.springframework.web.servlet.view;
1818

19-
import junit.framework.TestCase;
19+
import static org.junit.Assert.*;
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
2023
import org.springframework.mock.web.MockHttpServletRequest;
2124

2225
/**
23-
* Unit tests for the DefaultRequestToViewNameTranslator class.
24-
*
2526
* @author Rick Evans
27+
* @author Juergen Hoeller
2628
*/
27-
public final class DefaultRequestToViewNameTranslatorTests extends TestCase {
29+
public final class DefaultRequestToViewNameTranslatorTests {
2830

2931
private static final String VIEW_NAME = "apple";
3032
private static final String EXTENSION = ".html";
@@ -34,68 +36,86 @@ public final class DefaultRequestToViewNameTranslatorTests extends TestCase {
3436
private MockHttpServletRequest request;
3537

3638

37-
protected void setUp() throws Exception {
39+
@Before
40+
public void setUp() {
3841
this.translator = new DefaultRequestToViewNameTranslator();
3942
this.request = new MockHttpServletRequest();
4043
this.request.setContextPath(CONTEXT_PATH);
4144
}
4245

4346

44-
public void TODO_testGetViewNameLeavesLeadingSlashIfSoConfigured() throws Exception {
45-
request.setRequestURI(CONTEXT_PATH + VIEW_NAME);
47+
@Test
48+
public void testGetViewNameLeavesLeadingSlashIfSoConfigured() {
49+
request.setRequestURI(CONTEXT_PATH + "/" + VIEW_NAME + "/");
4650
this.translator.setStripLeadingSlash(false);
4751
assertViewName("/" + VIEW_NAME);
4852
}
4953

50-
public void testGetViewNameLeavesExtensionIfSoConfigured() throws Exception {
51-
request.setRequestURI(CONTEXT_PATH + VIEW_NAME + EXTENSION);
54+
@Test
55+
public void testGetViewNameLeavesTrailingSlashIfSoConfigured() {
56+
request.setRequestURI(CONTEXT_PATH + "/" + VIEW_NAME + "/");
57+
this.translator.setStripTrailingSlash(false);
58+
assertViewName(VIEW_NAME + "/");
59+
}
60+
61+
@Test
62+
public void testGetViewNameLeavesExtensionIfSoConfigured() {
63+
request.setRequestURI(CONTEXT_PATH + "/" + VIEW_NAME + EXTENSION);
5264
this.translator.setStripExtension(false);
5365
assertViewName(VIEW_NAME + EXTENSION);
5466
}
5567

56-
public void testGetViewNameWithDefaultConfiguration() throws Exception {
68+
@Test
69+
public void testGetViewNameWithDefaultConfiguration() {
5770
request.setRequestURI(CONTEXT_PATH + VIEW_NAME + EXTENSION);
5871
assertViewName(VIEW_NAME);
5972
}
6073

61-
public void testGetViewNameWithCustomSeparator() throws Exception {
74+
@Test
75+
public void testGetViewNameWithCustomSeparator() {
6276
request.setRequestURI(CONTEXT_PATH + VIEW_NAME + "/fiona" + EXTENSION);
6377
this.translator.setSeparator("_");
6478
assertViewName(VIEW_NAME + "_fiona");
6579
}
6680

67-
public void testGetViewNameWithNoExtension() throws Exception {
81+
@Test
82+
public void testGetViewNameWithNoExtension() {
6883
request.setRequestURI(CONTEXT_PATH + VIEW_NAME);
6984
assertViewName(VIEW_NAME);
7085
}
7186

72-
public void testGetViewNameWithPrefix() throws Exception {
87+
@Test
88+
public void testGetViewNameWithPrefix() {
7389
final String prefix = "fiona_";
7490
request.setRequestURI(CONTEXT_PATH + VIEW_NAME);
7591
this.translator.setPrefix(prefix);
7692
assertViewName(prefix + VIEW_NAME);
7793
}
7894

79-
public void testGetViewNameWithNullPrefix() throws Exception {
95+
@Test
96+
public void testGetViewNameWithNullPrefix() {
8097
request.setRequestURI(CONTEXT_PATH + VIEW_NAME);
8198
this.translator.setPrefix(null);
8299
assertViewName(VIEW_NAME);
83100
}
84101

85-
public void testGetViewNameWithSuffix() throws Exception {
102+
@Test
103+
public void testGetViewNameWithSuffix() {
86104
final String suffix = ".fiona";
87105
request.setRequestURI(CONTEXT_PATH + VIEW_NAME);
88106
this.translator.setSuffix(suffix);
89107
assertViewName(VIEW_NAME + suffix);
90108
}
91109

92-
public void testGetViewNameWithNullSuffix() throws Exception {
110+
@Test
111+
public void testGetViewNameWithNullSuffix() {
93112
request.setRequestURI(CONTEXT_PATH + VIEW_NAME);
94113
this.translator.setSuffix(null);
95114
assertViewName(VIEW_NAME);
96115
}
97116

98-
public void testTrySetUrlPathHelperToNull() throws Exception {
117+
@Test
118+
public void testTrySetUrlPathHelperToNull() {
99119
try {
100120
this.translator.setUrlPathHelper(null);
101121
}

0 commit comments

Comments
 (0)