Browse Source

Fix remote context handling in UrlTag

Prior to this change, the `UrlTag` would simply append the remote
context and the path value in case of a context relative URL.

The following code snippet would output "//foo":

```
<spring:url value="/foo" context="/" />
```

This change now removes trailing slashes in remote context to avoid
this.

Issue: SPR-12782
pull/788/merge
Brian Clozel 10 years ago
parent
commit
8b545f47bd
  1. 9
      spring-webmvc/src/main/java/org/springframework/web/servlet/tags/UrlTag.java
  2. 16
      spring-webmvc/src/test/java/org/springframework/web/servlet/tags/UrlTagTests.java

9
spring-webmvc/src/main/java/org/springframework/web/servlet/tags/UrlTag.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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.
@ -209,7 +209,12 @@ public class UrlTag extends HtmlEscapingAwareTag implements ParamAware { @@ -209,7 +209,12 @@ public class UrlTag extends HtmlEscapingAwareTag implements ParamAware {
url.append(request.getContextPath());
}
else {
url.append(this.context);
if(this.context.endsWith("/")) {
url.append(this.context.substring(0, this.context.length() - 1));
}
else {
url.append(this.context);
}
}
}
if (this.type != UrlType.RELATIVE && this.type != UrlType.ABSOLUTE && !this.value.startsWith("/")) {

16
spring-webmvc/src/test/java/org/springframework/web/servlet/tags/UrlTagTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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.
@ -528,6 +528,20 @@ public class UrlTagTests extends AbstractTagTests { @@ -528,6 +528,20 @@ public class UrlTagTests extends AbstractTagTests {
assertEquals("/some-other-context/url/path", uri);
}
public void testCreateUrlRemoteContextSingleSlash() throws JspException {
((MockHttpServletRequest) context.getRequest())
.setContextPath("/app-context");
tag.setValue("/url/path");
tag.setContext("/");
tag.doStartTag();
String uri = invokeCreateUrl(tag);
assertEquals("/url/path", uri);
}
public void testCreateUrlWithParams() throws JspException {
tag.setValue("url/path");

Loading…
Cancel
Save