Simple Context
Class de.odysseus.el.util.SimpleContext is a simple context implementation. It can be used at creation time as well as evaluation time.
For use at creation time, it provides the following methods.
- setFunction(String prefix, String name, java.lang.reflect.Method method) to define a method as a function for the given prefix and name. Functions without a namespace must pass in the empty string as prefix. The supplied method must be declared as public and static.
- setVariable(String name, javax.el.ValueExpression expression) to define a value expression as a variable for the given name. (This is equivalent to getVariableMapper().setVariable(String name, javax.el.ValueExpression expression).)
The following example defines function math:sin and variable pi and uses them in an expression.
import javax.el.*; import de.odysseus.el.util.SimpleContext; import de.odysseus.el.ExpressionFactoryImpl; ... ExpressionFactory factory = new ExpressionFactoryImpl(); SimpleContext context = new SimpleContext(); context.setFunction("math", "sin", Math.class.getMethod("sin", double.class)); context.setVariable("pi", factory.createValueExpression(Math.PI, double.class)); ValueExpression expr = factory.createValueExpression(context, "${math:sin(pi/2)}", double.class); System.out.println("math:sin(pi/2) = " + expr.getValue(context)); // 1.0
At evaluation time, a javax.el.ELResolver is required for property resolution and to resolve identifiers, that have not been bound to a variable. The getELResolver() method is used at evaluation time to access the context's resolver instance.
A resolver may be passed to a SimpleContext at construction time. If the default constructor is used, calling getELResolver() will lazily create an instance of de.odysseus.el.util.SimpleResolver.