fixed JSP EvalTag to render null result as empty String instead of "null" String (SPR-7459)
This commit is contained in:
parent
9bd0bd9c3a
commit
34ebad5fff
|
|
@ -32,6 +32,7 @@ import org.springframework.expression.TypedValue;
|
||||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||||
import org.springframework.expression.spel.support.StandardTypeConverter;
|
import org.springframework.expression.spel.support.StandardTypeConverter;
|
||||||
|
import org.springframework.util.ObjectUtils;
|
||||||
import org.springframework.web.util.ExpressionEvaluationUtils;
|
import org.springframework.web.util.ExpressionEvaluationUtils;
|
||||||
import org.springframework.web.util.HtmlUtils;
|
import org.springframework.web.util.HtmlUtils;
|
||||||
import org.springframework.web.util.JavaScriptUtils;
|
import org.springframework.web.util.JavaScriptUtils;
|
||||||
|
|
@ -103,21 +104,22 @@ public class EvalTag extends HtmlEscapingAwareTag {
|
||||||
if (this.evaluationContext == null) {
|
if (this.evaluationContext == null) {
|
||||||
this.evaluationContext = createEvaluationContext(pageContext);
|
this.evaluationContext = createEvaluationContext(pageContext);
|
||||||
}
|
}
|
||||||
if (this.var == null) {
|
if (this.var != null) {
|
||||||
|
Object result = this.expression.getValue(this.evaluationContext);
|
||||||
|
pageContext.setAttribute(this.var, result, this.scope);
|
||||||
|
}
|
||||||
|
else {
|
||||||
try {
|
try {
|
||||||
String result = this.expression.getValue(this.evaluationContext, String.class);
|
String result = this.expression.getValue(this.evaluationContext, String.class);
|
||||||
result = isHtmlEscape() ? HtmlUtils.htmlEscape(result) : result;
|
result = ObjectUtils.getDisplayString(result);
|
||||||
result = this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(result) : result;
|
result = (isHtmlEscape() ? HtmlUtils.htmlEscape(result) : result);
|
||||||
|
result = (this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(result) : result);
|
||||||
pageContext.getOut().print(result);
|
pageContext.getOut().print(result);
|
||||||
}
|
}
|
||||||
catch (IOException ex) {
|
catch (IOException ex) {
|
||||||
throw new JspException(ex);
|
throw new JspException(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
Object result = this.expression.getValue(this.evaluationContext);
|
|
||||||
pageContext.setAttribute(this.var, result, this.scope);
|
|
||||||
}
|
|
||||||
return EVAL_PAGE;
|
return EVAL_PAGE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -52,9 +52,18 @@ public class EvalTagTests extends AbstractTagTests {
|
||||||
assertEquals(Tag.EVAL_BODY_INCLUDE, action);
|
assertEquals(Tag.EVAL_BODY_INCLUDE, action);
|
||||||
action = tag.doEndTag();
|
action = tag.doEndTag();
|
||||||
assertEquals(Tag.EVAL_PAGE, action);
|
assertEquals(Tag.EVAL_PAGE, action);
|
||||||
assertEquals("foo", ((MockHttpServletResponse)context.getResponse()).getContentAsString());
|
assertEquals("foo", ((MockHttpServletResponse) context.getResponse()).getContentAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testPrintNullAsEmptyString() throws Exception {
|
||||||
|
tag.setExpression("bean.null");
|
||||||
|
int action = tag.doStartTag();
|
||||||
|
assertEquals(Tag.EVAL_BODY_INCLUDE, action);
|
||||||
|
action = tag.doEndTag();
|
||||||
|
assertEquals(Tag.EVAL_PAGE, action);
|
||||||
|
assertEquals("", ((MockHttpServletResponse) context.getResponse()).getContentAsString());
|
||||||
|
}
|
||||||
|
|
||||||
public void testPrintFormattedScopedAttributeResult() throws Exception {
|
public void testPrintFormattedScopedAttributeResult() throws Exception {
|
||||||
tag.setExpression("bean.formattable");
|
tag.setExpression("bean.formattable");
|
||||||
int action = tag.doStartTag();
|
int action = tag.doStartTag();
|
||||||
|
|
@ -63,7 +72,7 @@ public class EvalTagTests extends AbstractTagTests {
|
||||||
assertEquals(Tag.EVAL_PAGE, action);
|
assertEquals(Tag.EVAL_PAGE, action);
|
||||||
assertEquals("25%", ((MockHttpServletResponse) context.getResponse()).getContentAsString());
|
assertEquals("25%", ((MockHttpServletResponse) context.getResponse()).getContentAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testPrintHtmlEscapedAttributeResult() throws Exception {
|
public void testPrintHtmlEscapedAttributeResult() throws Exception {
|
||||||
tag.setExpression("bean.html()");
|
tag.setExpression("bean.html()");
|
||||||
tag.setHtmlEscape("true");
|
tag.setHtmlEscape("true");
|
||||||
|
|
@ -71,7 +80,7 @@ public class EvalTagTests extends AbstractTagTests {
|
||||||
assertEquals(Tag.EVAL_BODY_INCLUDE, action);
|
assertEquals(Tag.EVAL_BODY_INCLUDE, action);
|
||||||
action = tag.doEndTag();
|
action = tag.doEndTag();
|
||||||
assertEquals(Tag.EVAL_PAGE, action);
|
assertEquals(Tag.EVAL_PAGE, action);
|
||||||
assertEquals("<p>", ((MockHttpServletResponse)context.getResponse()).getContentAsString());
|
assertEquals("<p>", ((MockHttpServletResponse) context.getResponse()).getContentAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testPrintJavaScriptEscapedAttributeResult() throws Exception {
|
public void testPrintJavaScriptEscapedAttributeResult() throws Exception {
|
||||||
|
|
@ -133,10 +142,15 @@ public class EvalTagTests extends AbstractTagTests {
|
||||||
public String html() {
|
public String html() {
|
||||||
return "<p>";
|
return "<p>";
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getBean() {
|
public String getBean() {
|
||||||
return "not the bean object";
|
return "not the bean object";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Object getNull() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public String js() {
|
public String js() {
|
||||||
return "function foo() { alert(\"hi\") }";
|
return "function foo() { alert(\"hi\") }";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue