JSP EvalTag resolves "@myBeanName" references in expressions against the WebApplicationContext (SPR-7312); for consistency, expressions in an ApplicationContext support the same syntax against the local BeanFactory

This commit is contained in:
Juergen Hoeller
2010-07-12 19:36:26 +00:00
parent d1d5e2d52a
commit 263fabb0fc
6 changed files with 86 additions and 18 deletions

View File

@@ -21,6 +21,7 @@ import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.el.VariableResolver;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.core.convert.ConversionService;
import org.springframework.expression.AccessException;
import org.springframework.expression.EvaluationContext;
@@ -59,12 +60,6 @@ public class EvalTag extends HtmlEscapingAwareTag {
private boolean javaScriptEscape = false;
@Override
public void setPageContext(PageContext pageContext) {
super.setPageContext(pageContext);
this.evaluationContext = createEvaluationContext(pageContext);
}
/**
* Set the expression to evaluate.
*/
@@ -105,6 +100,9 @@ public class EvalTag extends HtmlEscapingAwareTag {
@Override
public int doEndTag() throws JspException {
if (this.evaluationContext == null) {
this.evaluationContext = createEvaluationContext(pageContext);
}
if (this.var == null) {
try {
String result = this.expression.getValue(this.evaluationContext, String.class);
@@ -123,10 +121,10 @@ public class EvalTag extends HtmlEscapingAwareTag {
return EVAL_PAGE;
}
private EvaluationContext createEvaluationContext(PageContext pageContext) {
StandardEvaluationContext context = new StandardEvaluationContext();
context.addPropertyAccessor(new JspPropertyAccessor(pageContext));
context.setBeanResolver(new BeanFactoryResolver(getRequestContext().getWebApplicationContext()));
ConversionService conversionService = getConversionService(pageContext);
if (conversionService != null) {
context.setTypeConverter(new StandardTypeConverter(conversionService));

View File

@@ -19,11 +19,13 @@ package org.springframework.web.servlet.tags;
import java.math.BigDecimal;
import javax.servlet.jsp.tagext.Tag;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.format.annotation.NumberFormat.Style;
import org.springframework.format.support.FormattingConversionServiceFactoryBean;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockPageContext;
import org.springframework.web.servlet.DispatcherServlet;
/**
* @author Keith Donald
@@ -103,6 +105,19 @@ public class EvalTagTests extends AbstractTagTests {
assertEquals("not the bean object", context.getAttribute("foo"));
}
public void testAccessUsingBeanSyntax() throws Exception {
GenericApplicationContext wac = (GenericApplicationContext)
context.getRequest().getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
wac.getDefaultListableBeanFactory().registerSingleton("bean2", context.getRequest().getAttribute("bean"));
tag.setExpression("@bean2.bean");
tag.setVar("foo");
int action = tag.doStartTag();
assertEquals(Tag.EVAL_BODY_INCLUDE, action);
action = tag.doEndTag();
assertEquals(Tag.EVAL_PAGE, action);
assertEquals("not the bean object", context.getAttribute("foo"));
}
public static class Bean {