Skip to content

Commit d95cbe2

Browse files
committed
added support for web.xml context-param "springJspExpressionSupport" (explicit "true"/"false")
1 parent ac1ffff commit d95cbe2

File tree

2 files changed

+66
-10
lines changed

2 files changed

+66
-10
lines changed

org.springframework.web/src/main/java/org/springframework/web/util/ExpressionEvaluationUtils.java

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.web.util;
1818

19+
import javax.servlet.ServletContext;
1920
import javax.servlet.jsp.JspException;
2021
import javax.servlet.jsp.PageContext;
2122
import javax.servlet.jsp.el.ELException;
@@ -30,18 +31,53 @@
3031
* invoking the EL evaluator, treating the value as "normal" expression
3132
* (i.e. a literal String value) else.
3233
*
34+
* <p><b>See {@link #isSpringJspExpressionSupportActive} for guidelines
35+
* on when to use Spring's JSP expression support as opposed to the
36+
* built-in expression support in JSP 2.0+ containers.</b>
37+
*
3338
* @author Juergen Hoeller
3439
* @author Alef Arendsen
3540
* @since 11.07.2003
3641
* @see javax.servlet.jsp.el.ExpressionEvaluator#evaluate
3742
*/
3843
public abstract class ExpressionEvaluationUtils {
3944

45+
/**
46+
* Expression support parameter at the servlet context level
47+
* (i.e. a context-param in <code>web.xml</code>): "springJspExpressionSupport".
48+
*/
49+
public static final String EXPRESSION_SUPPORT_CONTEXT_PARAM = "springJspExpressionSupport";
50+
4051
public static final String EXPRESSION_PREFIX = "${";
4152

4253
public static final String EXPRESSION_SUFFIX = "}";
4354

4455

56+
/**
57+
* Check whether Spring's JSP expression support is actually active.
58+
* <p>Note that JSP 2.0+ containers come with expression support themselves:
59+
* However, it will only be active for web applications declaring Servlet 2.4
60+
* or higher in their <code>web.xml</code> deployment descriptor.
61+
* <p>If a <code>web.xml</code> context-param named "springJspExpressionSupport" is
62+
* found, its boolean value will be taken to decide whether this support is active.
63+
* If not found, for backwards compatibility with Servlet 2.3 applications,
64+
* Spring's expression support will remain active by default.
65+
* <p><b>Recommendations:</b> Explicitly set "springJspExpressionSupport" to "false"
66+
* in order to prevent double evaluation for Servlet 2.4+ based applications.
67+
* @param pageContext current JSP PageContext
68+
* @return <code>true</code> if active (ExpressionEvaluationUtils will actually evaluate expressions);
69+
* <code>false</code> if not active (ExpressionEvaluationUtils will return given values as-is,
70+
* relying on the JSP container pre-evaluating values before passing them to JSP tag attributes)
71+
*/
72+
public static boolean isSpringJspExpressionSupportActive(PageContext pageContext) {
73+
ServletContext sc = pageContext.getServletContext();
74+
String springJspExpressionSupport = sc.getInitParameter(EXPRESSION_SUPPORT_CONTEXT_PARAM);
75+
if (springJspExpressionSupport != null) {
76+
return Boolean.valueOf(springJspExpressionSupport);
77+
}
78+
return true;
79+
}
80+
4581
/**
4682
* Check if the given expression value is an EL expression.
4783
* @param value the expression to check
@@ -67,7 +103,7 @@ public static boolean isExpressionLanguage(String value) {
67103
public static Object evaluate(String attrName, String attrValue, Class resultClass, PageContext pageContext)
68104
throws JspException {
69105

70-
if (isExpressionLanguage(attrValue)) {
106+
if (isSpringJspExpressionSupportActive(pageContext) && isExpressionLanguage(attrValue)) {
71107
return doEvaluate(attrName, attrValue, resultClass, pageContext);
72108
}
73109
else if (attrValue != null && resultClass != null && !resultClass.isInstance(attrValue)) {
@@ -90,7 +126,7 @@ else if (attrValue != null && resultClass != null && !resultClass.isInstance(att
90126
public static Object evaluate(String attrName, String attrValue, PageContext pageContext)
91127
throws JspException {
92128

93-
if (isExpressionLanguage(attrValue)) {
129+
if (isSpringJspExpressionSupportActive(pageContext) && isExpressionLanguage(attrValue)) {
94130
return doEvaluate(attrName, attrValue, Object.class, pageContext);
95131
}
96132
else {
@@ -109,7 +145,7 @@ public static Object evaluate(String attrName, String attrValue, PageContext pag
109145
public static String evaluateString(String attrName, String attrValue, PageContext pageContext)
110146
throws JspException {
111147

112-
if (isExpressionLanguage(attrValue)) {
148+
if (isSpringJspExpressionSupportActive(pageContext) && isExpressionLanguage(attrValue)) {
113149
return (String) doEvaluate(attrName, attrValue, String.class, pageContext);
114150
}
115151
else {
@@ -128,7 +164,7 @@ public static String evaluateString(String attrName, String attrValue, PageConte
128164
public static int evaluateInteger(String attrName, String attrValue, PageContext pageContext)
129165
throws JspException {
130166

131-
if (isExpressionLanguage(attrValue)) {
167+
if (isSpringJspExpressionSupportActive(pageContext) && isExpressionLanguage(attrValue)) {
132168
return (Integer) doEvaluate(attrName, attrValue, Integer.class, pageContext);
133169
}
134170
else {
@@ -147,15 +183,14 @@ public static int evaluateInteger(String attrName, String attrValue, PageContext
147183
public static boolean evaluateBoolean(String attrName, String attrValue, PageContext pageContext)
148184
throws JspException {
149185

150-
if (isExpressionLanguage(attrValue)) {
186+
if (isSpringJspExpressionSupportActive(pageContext) && isExpressionLanguage(attrValue)) {
151187
return (Boolean) doEvaluate(attrName, attrValue, Boolean.class, pageContext);
152188
}
153189
else {
154190
return Boolean.valueOf(attrValue);
155191
}
156192
}
157193

158-
159194
/**
160195
* Actually evaluate the given expression (be it EL or a literal String value)
161196
* to an Object of a given type. Supports concatenated expressions,

org.springframework.web/src/test/java/org/springframework/web/util/ExpressionEvaluationUtilsTests.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,25 +24,39 @@
2424
import javax.servlet.jsp.el.FunctionMapper;
2525
import javax.servlet.jsp.el.VariableResolver;
2626

27-
import junit.framework.TestCase;
27+
import org.junit.Test;
2828

2929
import org.springframework.mock.web.MockExpressionEvaluator;
3030
import org.springframework.mock.web.MockPageContext;
31+
import org.springframework.mock.web.MockServletContext;
32+
33+
import static org.junit.Assert.*;
3134

3235
/**
3336
* @author Aled Arendsen
3437
* @author Juergen Hoeller
3538
* @since 16.09.2003
3639
*/
37-
public class ExpressionEvaluationUtilsTests extends TestCase {
40+
public class ExpressionEvaluationUtilsTests {
41+
42+
@Test
43+
public void testIsSpringJspExpressionSupportActive() {
44+
MockServletContext sc = new MockServletContext();
45+
PageContext pc = new MockPageContext(sc);
46+
assertTrue(ExpressionEvaluationUtils.isSpringJspExpressionSupportActive(pc));
47+
sc.addInitParameter("springJspExpressionSupport", "false");
48+
assertFalse(ExpressionEvaluationUtils.isSpringJspExpressionSupportActive(pc));
49+
}
3850

51+
@Test
3952
public void testIsExpressionLanguage() {
4053
assertTrue(ExpressionEvaluationUtils.isExpressionLanguage("${bla}"));
4154
assertTrue(ExpressionEvaluationUtils.isExpressionLanguage("bla${bla}"));
4255
assertFalse(ExpressionEvaluationUtils.isExpressionLanguage("bla{bla"));
4356
assertFalse(ExpressionEvaluationUtils.isExpressionLanguage("bla$b{"));
4457
}
4558

59+
@Test
4660
public void testEvaluate() throws Exception {
4761
PageContext ctx = new MockPageContext();
4862
ctx.setAttribute("bla", "blie");
@@ -59,6 +73,7 @@ public void testEvaluate() throws Exception {
5973
}
6074
}
6175

76+
@Test
6277
public void testEvaluateWithConcatenation() throws Exception {
6378
PageContext ctx = new MockPageContext();
6479
ctx.setAttribute("bla", "blie");
@@ -98,6 +113,7 @@ public void testEvaluateWithConcatenation() throws Exception {
98113
}
99114
}
100115

116+
@Test
101117
public void testEvaluateString() throws Exception {
102118
PageContext ctx = new MockPageContext();
103119
ctx.setAttribute("bla", "blie");
@@ -106,6 +122,7 @@ public void testEvaluateString() throws Exception {
106122
assertEquals("blie", ExpressionEvaluationUtils.evaluateString("test", "blie", ctx));
107123
}
108124

125+
@Test
109126
public void testEvaluateStringWithConcatenation() throws Exception {
110127
PageContext ctx = new MockPageContext();
111128
ctx.setAttribute("bla", "blie");
@@ -137,6 +154,7 @@ public void testEvaluateStringWithConcatenation() throws Exception {
137154

138155
}
139156

157+
@Test
140158
public void testEvaluateInteger() throws Exception {
141159
PageContext ctx = new MockPageContext();
142160
ctx.setAttribute("bla", new Integer(1));
@@ -145,6 +163,7 @@ public void testEvaluateInteger() throws Exception {
145163
assertEquals(21, ExpressionEvaluationUtils.evaluateInteger("test", "21", ctx));
146164
}
147165

166+
@Test
148167
public void testEvaluateBoolean() throws Exception {
149168
PageContext ctx = new MockPageContext();
150169
ctx.setAttribute("bla", new Boolean(true));
@@ -153,6 +172,7 @@ public void testEvaluateBoolean() throws Exception {
153172
assertTrue(ExpressionEvaluationUtils.evaluateBoolean("test", "true", ctx));
154173
}
155174

175+
@Test
156176
public void testRepeatedEvaluate() throws Exception {
157177
PageContext ctx = new CountingMockPageContext();
158178
CountingMockExpressionEvaluator eval = (CountingMockExpressionEvaluator) ctx.getExpressionEvaluator();
@@ -172,6 +192,7 @@ public void testRepeatedEvaluate() throws Exception {
172192
assertEquals(4, eval.evaluateCount);
173193
}
174194

195+
@Test
175196
public void testEvaluateWithComplexConcatenation() throws Exception {
176197
PageContext ctx = new CountingMockPageContext();
177198
CountingMockExpressionEvaluator eval = (CountingMockExpressionEvaluator) ctx.getExpressionEvaluator();

0 commit comments

Comments
 (0)