【问题标题】:EL getType obj.property1.sub_propertyEL getType obj.property1.sub_property
【发布时间】:2014-03-07 01:37:19
【问题描述】:

我想获取对象“obj”的子属性的类型,可以访问为

obj.property1.sub_property

此代码仅适用于像 obj.property 这样的直接属性

FacesContext.getCurrentInstance().getApplication().
     getELResolver().getType(FacesContext.getCurrentInstance().getELContext(),
     obj, "property1");

但是如何做到这一点

FacesContext.getCurrentInstance().getApplication().
     getELResolver().getType(FacesContext.getCurrentInstance().getELContext(),
     obj, "property1.sub_property");

或者如何获取完整表达式#{obj.property1.sub_property}的TYPE

【问题讨论】:

    标签: java jsf-2 el


    【解决方案1】:

    仅当 obj.preperty1 不为空时才有效,除非我们有空指针异常

    new ExpressionFactoryImpl().createValueExpression(
         FacesUtils.getFacesContext().getELContext(),
         expr, Object.class).getType(
         FacesUtils.getFacesContext().getELContext())
    

    因为 kolossus 提到它会评估第一部分,然后是第二部分......

    【讨论】:

    • 如果这个答案对你有用,你需要添加一些措辞或解释。就目前而言,它的质量不是很好
    【解决方案2】:

    ELResolver 就是这样工作的。即使在幕后,每个成员也会被单独评估。

    来自规范:

    ELResolver概念是统一EL的核心……比如在渲染标签<h:outputText value=#{user.address.street}/>后面的组件时,ELResolver被调用了3次。一次解析user,再次解析用户的address属性,最后解析addressstreet属性

    翻译: 您需要分别评估property1subproperty。我是在手机上输入的,所以稍后我会添加代码示例

    【讨论】:

    • 错了,有时候你需要在没有评估的情况下获得类型,所以我发布的解决方案更好,谢谢你
    • @user3252285 - 我没有说一个比另一个好;如果你读到我在这里的内容,我说ELResolver我在解释ELResolver的行为
    • 但您说“您需要分别评估 property1 和 subproperty”,我的解决方案能够检测到更复杂的表达式,例如 #{bean.formatValue(obj.value)}
    • @user3252285 您需要评估每个如果您使用的是ELResolver。同样,不要批评您的解决方案
    • 你说得对,在内部,它评估每个 var 的一部分
    猜你喜欢
    • 1970-01-01
    • 2012-04-29
    • 2012-10-29
    • 2013-10-28
    • 1970-01-01
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多