From 741b4b229ae032bd17175b46f98673ce0bd2d485 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 13 Feb 2014 14:36:12 -0500 Subject: [PATCH] Add encoding for the default action in FormTag Issue: SPR-11426 --- .../web/servlet/tags/form/FormTag.java | 11 ++++++++++- .../web/servlet/tags/form/FormTagTests.java | 17 ++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/form/FormTag.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/form/FormTag.java index a6aa59c5576..8c50bde21d4 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/form/FormTag.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/form/FormTag.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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,6 +16,7 @@ package org.springframework.web.servlet.tags.form; +import java.io.UnsupportedEncodingException; import java.util.Map; import javax.servlet.ServletRequest; @@ -32,6 +33,7 @@ import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; import org.springframework.web.servlet.support.RequestDataValueProcessor; import org.springframework.web.util.HtmlUtils; +import org.springframework.web.util.UriUtils; /** * Databinding-aware JSP tag for rendering an HTML '{@code form}' whose @@ -442,6 +444,13 @@ public class FormTag extends AbstractHtmlElementTag { } else { String requestUri = getRequestContext().getRequestUri(); + String encoding = pageContext.getResponse().getCharacterEncoding(); + try { + requestUri = UriUtils.encodePath(requestUri, encoding); + } + catch (UnsupportedEncodingException e) { + throw new JspException(e); + } ServletResponse response = this.pageContext.getResponse(); if (response instanceof HttpServletResponse) { requestUri = ((HttpServletResponse) response).encodeURL(requestUri); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/tags/form/FormTagTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/tags/form/FormTagTests.java index 8fdcc1cc4b9..2612761a2b9 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/tags/form/FormTagTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/tags/form/FormTagTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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. @@ -340,6 +340,21 @@ public class FormTagTests extends AbstractHtmlElementTagTests { assertFormTagClosed(output); } + public void testDefaultActionEncoded() throws Exception { + + this.request.setRequestURI("/a b c"); + request.setQueryString(""); + + this.tag.doStartTag(); + this.tag.doEndTag(); + this.tag.doFinally(); + + String output = getOutput(); + String formOutput = getFormTag(output); + + assertContainsAttribute(formOutput, "action", "/a%20b%20c"); + } + private String getFormTag(String output) { int inputStart = output.indexOf("<", 1); int inputEnd = output.lastIndexOf(">", output.length() - 2);