1
1
/*
2
- * Copyright 2002-2009 the original author or authors.
2
+ * Copyright 2002-2011 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .web .util ;
18
18
19
+ import javax .servlet .ServletContext ;
19
20
import javax .servlet .jsp .JspException ;
20
21
import javax .servlet .jsp .PageContext ;
21
22
import javax .servlet .jsp .el .ELException ;
30
31
* invoking the EL evaluator, treating the value as "normal" expression
31
32
* (i.e. a literal String value) else.
32
33
*
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
+ *
33
38
* @author Juergen Hoeller
34
39
* @author Alef Arendsen
35
40
* @since 11.07.2003
36
41
* @see javax.servlet.jsp.el.ExpressionEvaluator#evaluate
37
42
*/
38
43
public abstract class ExpressionEvaluationUtils {
39
44
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
+
40
51
public static final String EXPRESSION_PREFIX = "${" ;
41
52
42
53
public static final String EXPRESSION_SUFFIX = "}" ;
43
54
44
55
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
+
45
81
/**
46
82
* Check if the given expression value is an EL expression.
47
83
* @param value the expression to check
@@ -67,7 +103,7 @@ public static boolean isExpressionLanguage(String value) {
67
103
public static Object evaluate (String attrName , String attrValue , Class resultClass , PageContext pageContext )
68
104
throws JspException {
69
105
70
- if (isExpressionLanguage (attrValue )) {
106
+ if (isSpringJspExpressionSupportActive ( pageContext ) && isExpressionLanguage (attrValue )) {
71
107
return doEvaluate (attrName , attrValue , resultClass , pageContext );
72
108
}
73
109
else if (attrValue != null && resultClass != null && !resultClass .isInstance (attrValue )) {
@@ -90,7 +126,7 @@ else if (attrValue != null && resultClass != null && !resultClass.isInstance(att
90
126
public static Object evaluate (String attrName , String attrValue , PageContext pageContext )
91
127
throws JspException {
92
128
93
- if (isExpressionLanguage (attrValue )) {
129
+ if (isSpringJspExpressionSupportActive ( pageContext ) && isExpressionLanguage (attrValue )) {
94
130
return doEvaluate (attrName , attrValue , Object .class , pageContext );
95
131
}
96
132
else {
@@ -109,7 +145,7 @@ public static Object evaluate(String attrName, String attrValue, PageContext pag
109
145
public static String evaluateString (String attrName , String attrValue , PageContext pageContext )
110
146
throws JspException {
111
147
112
- if (isExpressionLanguage (attrValue )) {
148
+ if (isSpringJspExpressionSupportActive ( pageContext ) && isExpressionLanguage (attrValue )) {
113
149
return (String ) doEvaluate (attrName , attrValue , String .class , pageContext );
114
150
}
115
151
else {
@@ -128,7 +164,7 @@ public static String evaluateString(String attrName, String attrValue, PageConte
128
164
public static int evaluateInteger (String attrName , String attrValue , PageContext pageContext )
129
165
throws JspException {
130
166
131
- if (isExpressionLanguage (attrValue )) {
167
+ if (isSpringJspExpressionSupportActive ( pageContext ) && isExpressionLanguage (attrValue )) {
132
168
return (Integer ) doEvaluate (attrName , attrValue , Integer .class , pageContext );
133
169
}
134
170
else {
@@ -147,15 +183,14 @@ public static int evaluateInteger(String attrName, String attrValue, PageContext
147
183
public static boolean evaluateBoolean (String attrName , String attrValue , PageContext pageContext )
148
184
throws JspException {
149
185
150
- if (isExpressionLanguage (attrValue )) {
186
+ if (isSpringJspExpressionSupportActive ( pageContext ) && isExpressionLanguage (attrValue )) {
151
187
return (Boolean ) doEvaluate (attrName , attrValue , Boolean .class , pageContext );
152
188
}
153
189
else {
154
190
return Boolean .valueOf (attrValue );
155
191
}
156
192
}
157
193
158
-
159
194
/**
160
195
* Actually evaluate the given expression (be it EL or a literal String value)
161
196
* to an Object of a given type. Supports concatenated expressions,
0 commit comments