org.springframework.web.util
abstract public class: ExpressionEvaluationUtils [javadoc |
source]
java.lang.Object
org.springframework.web.util.ExpressionEvaluationUtils
Convenience methods for transparent access to JSP 2.0's built-in
javax.servlet.jsp.el.ExpressionEvaluator or the standalone
org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager
of Jakarta's JSTL implementation.
Automatically detects JSP 2.0 or Jakarta JSTL, preferring the JSP 2.0
mechanism if available. Also detects the JSP 2.0 API being present but
the runtime JSP engine not actually supporting JSP 2.0, falling back to
the Jakarta JSTL in this case. Throws an exception when encountering actual
EL expressions if neither JSP 2.0 nor the Jakarta JSTL is available.
In the case of JSP 2.0, this class will by default use standard
evaluate calls. If your application server happens to be
inefficient in that respect, consider setting Spring's "cacheJspExpressions"
context-param in web.xml to "true", which will use
parseExpression calls with cached Expression objects instead.
The evaluation methods check if the value contains "${" before
invoking the EL evaluator, treating the value as "normal" expression
(i.e. a literal String value) else.
Note: The evaluation methods do not have a runtime dependency on
JSP 2.0 or on Jakarta's JSTL implementation, as long as they are not
asked to process actual EL expressions. This allows for using EL-aware
tags with Java-based JSP expressions instead, for example.
| Field Summary |
|---|
| public static final String | EXPRESSION_CACHE_CONTEXT_PARAM | JSP 2.0 expression cache parameter at the servlet context level
(i.e. a context-param in web.xml): "cacheJspExpressions". |
| public static final String | EXPRESSION_PREFIX | |
| public static final String | EXPRESSION_SUFFIX | |
| Method from org.springframework.web.util.ExpressionEvaluationUtils Detail: |
public static Object evaluate(String attrName,
String attrValue,
PageContext pageContext) throws JspException {
if (isExpressionLanguage(attrValue)) {
return doEvaluate(attrName, attrValue, Object.class, pageContext);
}
else {
return attrValue;
}
}
Evaluate the given expression (be it EL or a literal String value) to an Object. |
public static Object evaluate(String attrName,
String attrValue,
Class resultClass,
PageContext pageContext) throws JspException {
if (isExpressionLanguage(attrValue)) {
return doEvaluate(attrName, attrValue, resultClass, pageContext);
}
else if (attrValue != null && resultClass != null && !resultClass.isInstance(attrValue)) {
throw new JspException("Attribute value \"" + attrValue + "\" is neither a JSP EL expression nor " +
"assignable to result class [" + resultClass.getName() + "]");
}
else {
return attrValue;
}
}
Evaluate the given expression (be it EL or a literal String value)
to an Object of a given type, |
public static boolean evaluateBoolean(String attrName,
String attrValue,
PageContext pageContext) throws JspException {
if (isExpressionLanguage(attrValue)) {
return ((Boolean) doEvaluate(attrName, attrValue, Boolean.class, pageContext)).booleanValue();
}
else {
return Boolean.valueOf(attrValue).booleanValue();
}
}
Evaluate the given expression (be it EL or a literal String value) to a boolean. |
public static int evaluateInteger(String attrName,
String attrValue,
PageContext pageContext) throws JspException {
if (isExpressionLanguage(attrValue)) {
return ((Integer) doEvaluate(attrName, attrValue, Integer.class, pageContext)).intValue();
}
else {
return Integer.parseInt(attrValue);
}
}
Evaluate the given expression (be it EL or a literal String value) to an integer. |
public static String evaluateString(String attrName,
String attrValue,
PageContext pageContext) throws JspException {
if (isExpressionLanguage(attrValue)) {
return (String) doEvaluate(attrName, attrValue, String.class, pageContext);
}
else {
return attrValue;
}
}
Evaluate the given expression (be it EL or a literal String value) to a String. |
public static boolean isExpressionLanguage(String value) {
ClassLoader cl = ExpressionEvaluationUtils.class.getClassLoader();
if (ClassUtils.isPresent(JSP_20_CLASS_NAME, cl)) {
logger.debug("Found JSP 2.0 ExpressionEvaluator");
if (ClassUtils.isPresent(JAKARTA_JSTL_CLASS_NAME, cl)) {
logger.debug("Found Jakarta JSTL ExpressionEvaluatorManager");
helper = new Jsp20ExpressionEvaluationHelper(new JakartaExpressionEvaluationHelper());
}
else {
helper = new Jsp20ExpressionEvaluationHelper(new NoExpressionEvaluationHelper());
}
}
else if (ClassUtils.isPresent(JAKARTA_JSTL_CLASS_NAME, cl)) {
logger.debug("Found Jakarta JSTL ExpressionEvaluatorManager");
helper = new JakartaExpressionEvaluationHelper();
}
else {
logger.debug("JSP expression evaluation not available");
helper = new NoExpressionEvaluationHelper();
}
return (value != null && value.indexOf(EXPRESSION_PREFIX) != -1);
}
Check if the given expression value is an EL expression. |