在JSF页面中从bean评估EL表达式 [英] Evaluate EL expression from bean in JSF page

查看:182
本文介绍了在JSF页面中从bean评估EL表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以执行从JSF页中的bean接收到的EL表达式?

Is there any way to execute EL expression received from bean in JSF page?

Bean方法

public class Bean {

    public String getExpr() {
        return "#{emtpy row.prop ? row.anotherProp : row.prop}";
    }

}

JSF页面:

<p:dataTable value="#{bean.items}" var="row">
    <p:column>
        <h:outputText value="#{bean.expr}" />
    </p:column>
</p:dataTable>


推荐答案

要么使用JSF Application#evaluateExpressionGet() 在getter中以编程方式在当前上下文中评估EL表达式。在这种特定情况下,这只是可恶的,因为您基本上是在控制器/模型中紧密耦合视图逻辑。

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);
}

或使用JSTL < c:set> (没有 scope !),以便在视图中创建EL表达式的别名,以防您真正关心的是EL表达式的长度。

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>

不用说,JSTL方法更简洁。

Needless to say that JSTL way is way much cleaner.

  • Defining and reusing an EL variable in JSF page
  • JSTL in JSF2 Facelets... makes sense?

这篇关于在JSF页面中从bean评估EL表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆