djmj
djmj

Reputation: 5544

evaluate EL expression within String value

Is it possible to evaluate an el-expression that is saved within a String value for instance in a managed bean variable?

In short: Is double evaluation possible?

@ManagedBean(name = "bean")
@ViewScoped
public class Bean 
{
    private final String name = "bar";
    private final String nameEl = "foo #{bean.name} foo #{bean.name}";

    public String getName()
    {
        return this.name;
    }

    public String getNameEl()
    {
        return this.nameEl;
    }
}

<h:outputText value="#{bean.nameEl}" escape="false"/>

The output is: foo #{bean.name} foo #{bean.name}

and wished output should be: foo bar foo bar

Idea:

An el function could solve this.

Upvotes: 3

Views: 4046

Answers (1)

BalusC
BalusC

Reputation: 1108557

You can use Application#evaluateExpressionGet() to evaluate a string as an EL expression and get the outcome.

FacesContext context = FacesContext.getCurrentInstance();
Object result = context.getApplication().evaluateExpressionGet(context, nameEL, Object.class);
// ...

In OmniFaces, this is also available in Faces class.

Object result = Faces.evaluateExpressionGet(nameEL);
// ...

Upvotes: 4

Related Questions