Skip to content

Commit e622555

Browse files
committed
Encode based on response character encoding
Before this commit, characters were always encoded with the default encoding (i.e. ISO-8859-1). Now, the character encoding of the response is used. Closes gh-files
1 parent c72e31b commit e622555

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/tags/form/AbstractFormTag.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ protected final int doStartTagInternal() throws Exception {
9292
* as required. This version is <strong>not</strong> {@link PropertyEditor}-aware.
9393
*/
9494
protected String getDisplayString(@Nullable Object value) {
95-
return ValueFormatter.getDisplayString(value, isHtmlEscape());
95+
String displayString = ValueFormatter.getDisplayString(value, false);
96+
return isHtmlEscape() ? htmlEscape(displayString) : displayString;
9697
}
9798

9899
/**
@@ -102,7 +103,8 @@ protected String getDisplayString(@Nullable Object value) {
102103
* to obtain the display value.
103104
*/
104105
protected String getDisplayString(@Nullable Object value, @Nullable PropertyEditor propertyEditor) {
105-
return ValueFormatter.getDisplayString(value, propertyEditor, isHtmlEscape());
106+
String displayString = ValueFormatter.getDisplayString(value, propertyEditor, false);
107+
return isHtmlEscape() ? htmlEscape(displayString) : displayString;
106108
}
107109

108110
/**

spring-webmvc/src/test/java/org/springframework/web/servlet/tags/form/InputTagTests.java

+21-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ void simpleBindTagWithinForm() throws Exception {
9696

9797
@Test
9898
void simpleBindWithHtmlEscaping() throws Exception {
99-
final String NAME = "Rob \"I Love Mangos\" Harrop";
100-
final String HTML_ESCAPED_NAME = "Rob &quot;I Love Mangos&quot; Harrop";
99+
final String NAME = "Rob \"I Love Cafés\" Harrop";
100+
final String HTML_ESCAPED_NAME = "Rob &quot;I Love Caf&eacute;s&quot; Harrop";
101101

102102
this.tag.setPath("name");
103103
this.rob.setName(NAME);
@@ -112,6 +112,25 @@ void simpleBindWithHtmlEscaping() throws Exception {
112112
assertValueAttribute(output, HTML_ESCAPED_NAME);
113113
}
114114

115+
@Test
116+
void simpleBindWithHtmlEscapingAndCharacterEncoding() throws Exception {
117+
final String NAME = "Rob \"I Love Cafés\" Harrop";
118+
final String HTML_ESCAPED_NAME = "Rob &quot;I Love Cafés&quot; Harrop";
119+
120+
this.getPageContext().getResponse().setCharacterEncoding("UTF-8");
121+
this.tag.setPath("name");
122+
this.rob.setName(NAME);
123+
124+
assertThat(this.tag.doStartTag()).isEqualTo(Tag.SKIP_BODY);
125+
126+
String output = getOutput();
127+
assertTagOpened(output);
128+
assertTagClosed(output);
129+
130+
assertContainsAttribute(output, "type", getType());
131+
assertValueAttribute(output, HTML_ESCAPED_NAME);
132+
}
133+
115134
protected void assertValueAttribute(String output, String expectedValue) {
116135
assertContainsAttribute(output, "value", expectedValue);
117136
}

0 commit comments

Comments
 (0)