fixed EvalTag's EvaluationContext caching (SPR-7482)

master
Juergen Hoeller 14 years ago
parent 3e5aca86d5
commit 5ddf8245dd
  1. 25
      org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/tags/EvalTag.java
  2. 6
      org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/tags/RequestContextAwareTag.java

@ -48,9 +48,15 @@ import org.springframework.web.util.TagUtils;
*/ */
public class EvalTag extends HtmlEscapingAwareTag { public class EvalTag extends HtmlEscapingAwareTag {
private final ExpressionParser expressionParser = new SpelExpressionParser(); /**
* {@link javax.servlet.jsp.PageContext} attribute for the
* page-level {@link EvaluationContext} instance.
*/
private static final String EVALUATION_CONTEXT_PAGE_ATTRIBUTE =
"org.springframework.web.servlet.tags.EVALUATION_CONTEXT";
private EvaluationContext evaluationContext;
private final ExpressionParser expressionParser = new SpelExpressionParser();
private Expression expression; private Expression expression;
@ -101,20 +107,23 @@ public class EvalTag extends HtmlEscapingAwareTag {
@Override @Override
public int doEndTag() throws JspException { public int doEndTag() throws JspException {
if (this.evaluationContext == null) { EvaluationContext evaluationContext =
this.evaluationContext = createEvaluationContext(pageContext); (EvaluationContext) this.pageContext.getAttribute(EVALUATION_CONTEXT_PAGE_ATTRIBUTE);
if (evaluationContext == null) {
evaluationContext = createEvaluationContext(this.pageContext);
this.pageContext.setAttribute(EVALUATION_CONTEXT_PAGE_ATTRIBUTE, evaluationContext);
} }
if (this.var != null) { if (this.var != null) {
Object result = this.expression.getValue(this.evaluationContext); Object result = this.expression.getValue(evaluationContext);
pageContext.setAttribute(this.var, result, this.scope); this.pageContext.setAttribute(this.var, result, this.scope);
} }
else { else {
try { try {
String result = this.expression.getValue(this.evaluationContext, String.class); String result = this.expression.getValue(evaluationContext, String.class);
result = ObjectUtils.getDisplayString(result); result = ObjectUtils.getDisplayString(result);
result = (isHtmlEscape() ? HtmlUtils.htmlEscape(result) : result); result = (isHtmlEscape() ? HtmlUtils.htmlEscape(result) : result);
result = (this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(result) : result); result = (this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(result) : result);
pageContext.getOut().print(result); this.pageContext.getOut().print(result);
} }
catch (IOException ex) { catch (IOException ex) {
throw new JspException(ex); throw new JspException(ex);

@ -48,8 +48,8 @@ import org.springframework.web.servlet.support.RequestContext;
public abstract class RequestContextAwareTag extends TagSupport implements TryCatchFinally { public abstract class RequestContextAwareTag extends TagSupport implements TryCatchFinally {
/** /**
* {@link javax.servlet.jsp.PageContext} attribute for page-level * {@link javax.servlet.jsp.PageContext} attribute for the
* {@link RequestContext} instance. * page-level {@link RequestContext} instance.
*/ */
public static final String REQUEST_CONTEXT_PAGE_ATTRIBUTE = public static final String REQUEST_CONTEXT_PAGE_ATTRIBUTE =
"org.springframework.web.servlet.tags.REQUEST_CONTEXT"; "org.springframework.web.servlet.tags.REQUEST_CONTEXT";
@ -70,8 +70,8 @@ public abstract class RequestContextAwareTag extends TagSupport implements TryCa
*/ */
@Override @Override
public final int doStartTag() throws JspException { public final int doStartTag() throws JspException {
this.requestContext = (RequestContext) this.pageContext.getAttribute(REQUEST_CONTEXT_PAGE_ATTRIBUTE);
try { try {
this.requestContext = (RequestContext) this.pageContext.getAttribute(REQUEST_CONTEXT_PAGE_ATTRIBUTE);
if (this.requestContext == null) { if (this.requestContext == null) {
this.requestContext = new JspAwareRequestContext(this.pageContext); this.requestContext = new JspAwareRequestContext(this.pageContext);
this.pageContext.setAttribute(REQUEST_CONTEXT_PAGE_ATTRIBUTE, this.requestContext); this.pageContext.setAttribute(REQUEST_CONTEXT_PAGE_ATTRIBUTE, this.requestContext);

Loading…
Cancel
Save