How can we resolve JSF EL expressions programmatically on Seam managed beans? I have tried the following, but it does not work:
private String resolveExpression(String expression){
if(expression == null)
{
return null;
}
javax.faces.context.FacesContext facesCtx = FacesContext.getCurrentInstance();
Application app = facesCtx.getApplication();
try
{
// Here we bind a value expression into the item,
// so it can dynamically change its language
ELContext elCtx = facesCtx.getELContext();
ExpressionFactory ef = app.getExpressionFactory();
ValueExpression ve = ef.createValueExpression(elCtx, expression, String.class);
return (String) ve.getValue(elCtx);
}
catch (ELException ex) {
}
return expression;
}
In my application I have a bean named User that in session scope of Seam sits and this bean has a property name. The EL expression is #{usr.name}, but this expression returns empty while it works fine in a Facelet file.
Seam provides two utility components for dealing with EL expressions:
ExpressionsandInterpolator.For your particular use case, I believe
Interpolatoris the correct choice, since you don’t really need a validValueExpression.Interpolatorparses the whole string for EL expressions and converts all of them to their evaluated values.Beware that
Interpolator#interpolate()also accepts arguments (see the documentation).Example: