From 564f0ec3c3f1c673cf15e6a76913bff4cf231a45 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 21 Jun 2011 16:50:00 +0000 Subject: [PATCH] SPR-8444 Added MapAccessor and EnvironmentAccessor to EL context in the eval tag --- .../web/servlet/tags/EvalTag.java | 5 +++ .../web/servlet/tags/EvalTagTests.java | 36 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/tags/EvalTag.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/tags/EvalTag.java index a4092d50d7e..2d3e74b5247 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/tags/EvalTag.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/tags/EvalTag.java @@ -17,11 +17,14 @@ package org.springframework.web.servlet.tags; import java.io.IOException; + 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.context.expression.EnvironmentAccessor; +import org.springframework.context.expression.MapAccessor; import org.springframework.core.convert.ConversionService; import org.springframework.expression.AccessException; import org.springframework.expression.EvaluationContext; @@ -135,6 +138,8 @@ public class EvalTag extends HtmlEscapingAwareTag { private EvaluationContext createEvaluationContext(PageContext pageContext) { StandardEvaluationContext context = new StandardEvaluationContext(); context.addPropertyAccessor(new JspPropertyAccessor(pageContext)); + context.addPropertyAccessor(new MapAccessor()); + context.addPropertyAccessor(new EnvironmentAccessor()); context.setBeanResolver(new BeanFactoryResolver(getRequestContext().getWebApplicationContext())); ConversionService conversionService = getConversionService(pageContext); if (conversionService != null) { diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/EvalTagTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/EvalTagTests.java index eca42cf0ad7..2759884aa4b 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/EvalTagTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/EvalTagTests.java @@ -17,9 +17,14 @@ package org.springframework.web.servlet.tags; import java.math.BigDecimal; +import java.util.HashMap; +import java.util.Map; + import javax.servlet.jsp.tagext.Tag; import org.springframework.context.support.GenericApplicationContext; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.PropertySource; import org.springframework.format.annotation.NumberFormat; import org.springframework.format.annotation.NumberFormat.Style; import org.springframework.format.support.FormattingConversionServiceFactoryBean; @@ -127,6 +132,31 @@ public class EvalTagTests extends AbstractTagTests { assertEquals("not the bean object", context.getAttribute("foo")); } + public void testEnvironmentAccess() throws Exception { + Map map = new HashMap(); + map.put("key.foo", "value.foo"); + GenericApplicationContext wac = (GenericApplicationContext) + context.getRequest().getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE); + wac.getEnvironment().getPropertySources().addFirst(new MapPropertySource("mapSource", map)); + wac.getDefaultListableBeanFactory().registerSingleton("bean2", context.getRequest().getAttribute("bean")); + tag.setExpression("@environment['key.foo']"); + int action = tag.doStartTag(); + assertEquals(Tag.EVAL_BODY_INCLUDE, action); + action = tag.doEndTag(); + assertEquals(Tag.EVAL_PAGE, action); + assertEquals("value.foo", ((MockHttpServletResponse) context.getResponse()).getContentAsString()); + } + + public void testMapAccess() throws Exception { + tag.setExpression("bean.map.key"); + int action = tag.doStartTag(); + assertEquals(Tag.EVAL_BODY_INCLUDE, action); + action = tag.doEndTag(); + assertEquals(Tag.EVAL_PAGE, action); + assertEquals("value", ((MockHttpServletResponse) context.getResponse()).getContentAsString()); + } + + public static class Bean { @@ -154,6 +184,12 @@ public class EvalTagTests extends AbstractTagTests { public String js() { return "function foo() { alert(\"hi\") }"; } + + public Map getMap() { + Map map = new HashMap(); + map.put("key", "value"); + return map; + } } }