From 8b545f47bd625aba2cd7d4d4877498d5a28f4f28 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Wed, 6 May 2015 14:54:18 +0200 Subject: [PATCH] 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": ``` ``` This change now removes trailing slashes in remote context to avoid this. Issue: SPR-12782 --- .../springframework/web/servlet/tags/UrlTag.java | 9 +++++++-- .../web/servlet/tags/UrlTagTests.java | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/UrlTag.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/UrlTag.java index c8713386c6c..6e8aaff12b7 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/UrlTag.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/UrlTag.java @@ -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 { 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("/")) { diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/tags/UrlTagTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/tags/UrlTagTests.java index 0ce1d6adc33..15ce4126775 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/tags/UrlTagTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/tags/UrlTagTests.java @@ -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 { 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");