Skip to content

Commit 34ebad5

Browse files
committed
fixed JSP EvalTag to render null result as empty String instead of "null" String (SPR-7459)
1 parent 9bd0bd9 commit 34ebad5

File tree

2 files changed

+27
-11
lines changed
  • org.springframework.web.servlet/src

2 files changed

+27
-11
lines changed

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/tags/EvalTag.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.expression.spel.standard.SpelExpressionParser;
3333
import org.springframework.expression.spel.support.StandardEvaluationContext;
3434
import org.springframework.expression.spel.support.StandardTypeConverter;
35+
import org.springframework.util.ObjectUtils;
3536
import org.springframework.web.util.ExpressionEvaluationUtils;
3637
import org.springframework.web.util.HtmlUtils;
3738
import org.springframework.web.util.JavaScriptUtils;
@@ -103,21 +104,22 @@ public int doEndTag() throws JspException {
103104
if (this.evaluationContext == null) {
104105
this.evaluationContext = createEvaluationContext(pageContext);
105106
}
106-
if (this.var == null) {
107+
if (this.var != null) {
108+
Object result = this.expression.getValue(this.evaluationContext);
109+
pageContext.setAttribute(this.var, result, this.scope);
110+
}
111+
else {
107112
try {
108113
String result = this.expression.getValue(this.evaluationContext, String.class);
109-
result = isHtmlEscape() ? HtmlUtils.htmlEscape(result) : result;
110-
result = this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(result) : result;
114+
result = ObjectUtils.getDisplayString(result);
115+
result = (isHtmlEscape() ? HtmlUtils.htmlEscape(result) : result);
116+
result = (this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(result) : result);
111117
pageContext.getOut().print(result);
112118
}
113119
catch (IOException ex) {
114120
throw new JspException(ex);
115121
}
116122
}
117-
else {
118-
Object result = this.expression.getValue(this.evaluationContext);
119-
pageContext.setAttribute(this.var, result, this.scope);
120-
}
121123
return EVAL_PAGE;
122124
}
123125

org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/EvalTagTests.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,18 @@ public void testPrintScopedAttributeResult() throws Exception {
5252
assertEquals(Tag.EVAL_BODY_INCLUDE, action);
5353
action = tag.doEndTag();
5454
assertEquals(Tag.EVAL_PAGE, action);
55-
assertEquals("foo", ((MockHttpServletResponse)context.getResponse()).getContentAsString());
55+
assertEquals("foo", ((MockHttpServletResponse) context.getResponse()).getContentAsString());
5656
}
57-
57+
58+
public void testPrintNullAsEmptyString() throws Exception {
59+
tag.setExpression("bean.null");
60+
int action = tag.doStartTag();
61+
assertEquals(Tag.EVAL_BODY_INCLUDE, action);
62+
action = tag.doEndTag();
63+
assertEquals(Tag.EVAL_PAGE, action);
64+
assertEquals("", ((MockHttpServletResponse) context.getResponse()).getContentAsString());
65+
}
66+
5867
public void testPrintFormattedScopedAttributeResult() throws Exception {
5968
tag.setExpression("bean.formattable");
6069
int action = tag.doStartTag();
@@ -63,15 +72,15 @@ public void testPrintFormattedScopedAttributeResult() throws Exception {
6372
assertEquals(Tag.EVAL_PAGE, action);
6473
assertEquals("25%", ((MockHttpServletResponse) context.getResponse()).getContentAsString());
6574
}
66-
75+
6776
public void testPrintHtmlEscapedAttributeResult() throws Exception {
6877
tag.setExpression("bean.html()");
6978
tag.setHtmlEscape("true");
7079
int action = tag.doStartTag();
7180
assertEquals(Tag.EVAL_BODY_INCLUDE, action);
7281
action = tag.doEndTag();
7382
assertEquals(Tag.EVAL_PAGE, action);
74-
assertEquals("<p>", ((MockHttpServletResponse)context.getResponse()).getContentAsString());
83+
assertEquals("<p>", ((MockHttpServletResponse) context.getResponse()).getContentAsString());
7584
}
7685

7786
public void testPrintJavaScriptEscapedAttributeResult() throws Exception {
@@ -133,10 +142,15 @@ public BigDecimal getFormattable() {
133142
public String html() {
134143
return "<p>";
135144
}
145+
136146
public String getBean() {
137147
return "not the bean object";
138148
}
139149

150+
public Object getNull() {
151+
return null;
152+
}
153+
140154
public String js() {
141155
return "function foo() { alert(\"hi\") }";
142156
}

0 commit comments

Comments
 (0)