Is there any way to execute EL expression received from bean in JSF page?
Bean method
public class Bean {
public String getExpr() {
return "#{emtpy row.prop ? row.anotherProp : row.prop}";
}
}
JSF page:
<p:dataTable value="#{bean.items}" var="row">
<p:column>
<h:outputText value="#{bean.expr}" />
</p:column>
</p:dataTable>
Either use JSF Application#evaluateExpressionGet()
in getter to programmatically evaluate an EL expression on the current context. This is in this specific case only fishy as you're basically tight-coupling view logic in the controller/model.
public String getExpr() {
FacesContext context = FacesContext.getCurrentInstance();
return context.getApplication().evaluateExpressionGet(context, "#{emtpy row.prop ? row.anotherProp : row.prop}", String.class);
}
Or use JSTL <c:set>
(without scope
!) in view to create an alias of an EL expression in the view in case your actual concern is the length of the EL expression.
<c:set var="expr" value="#{emtpy row.prop ? row.anotherProp : row.prop}" />
<p:dataTable value="#{bean.items}" var="row">
<p:column>
<h:outputText value="#{expr}" />
</p:column>
</p:dataTable>
Needless to say that JSTL way is way much cleaner.