There are not much examples, how to use a ELExpression from your java code. The most examples I found, are focussing on „implement your own ELResolver“. The best documentation comes from SUN in form of tutorials and package/javadoc.
Now I will provide some tips how to deal with EL expressions from java. I have tested and implemented this in a JSF 1.2 web application, where I have access to the FacesContext.
In JSF 1.2 you have access to expression factory:
// get application from faces context Application app = FacesContext.getInstance().getApplication(); ExpressionFactory exprFactory = app.getExpressionFactory();
Now you can use the expression factory to get a MethodExpression or ValueExpression. In my projects we need very often ValueExpressions.
But first you need also an ELContext. There are many implementations available of such context (for JSP, Faces and so on) and enough documentation how to create your own context.
You can access such context again from the faces context:
// getting the ELContext from faces context ELContext elContext = FacesContext.getInstance().getELContext(); // creating value expression with the help of the expression factory and the ELContext ValueExpression valExpr = exprFactory.createValueExpression(elContext, "#{devBean.devMode}", Boolean.class);
In this example I will use the boolean value from a „devBean“ to work later with this value. Of course, you can inject the value expression, required class type and so on.
The last needed thing is to assign the value to a local variable:
Boolean developmentMode = (Boolean) valExpr.getValue(elContext);
Thats it!