Evaluate an EL expression

On February 8, 2011, in java, by Kishore

Once you start using EL heavily within your UI, eventually you will want to use it/evaluate it back in your EJB’s and Seam/Spring components

There are three steps to evaluating an EL expression.

1
2
3
4
5
6
7
8
9
10
11
//get current EL context
javax.el.ELContext elContext = javax.faces.context.FacesContext.getCurrentInstance().getELContext();
 
//get the expression factory (for seam). You can probably do ExpressionFactory.newInstance() if not using seam .
javax.el.ExpressionFactory expressionFactory = org.jboss.seam.core.Expressions.instance().getExpressionFactory();
 
//Create value expression as the EL I'm evaluating is a value e.g. #{bean.property} . Create MethodExpression if the EL is a method e.g. #{bean.method()}
javax.el.ValueExpression valueExpression = expressionFactory.createValueExpression(elContext, elExpressionYouAreEvaluating, WhateverYouAreExpecting.class);
 
// get value and dont' forget to cast.
whateverYouAreExpecting = (WhateverYouAreExpecting) valueExpression.getValue(elContext);

Yup.. that’s how convulted this is . It should more be like below

1
2
//NOTE: imaginary code
Object value = ExpressionFactory.getValue("#{bean.property}");

That’s what these guys are asking as well..

Tagged with:
 

Leave a Reply