4

我想使用值表达式的动态参数来实现这一点:

<h:dataTable value="#{someBean.someValue}" var="field">
    <h:column>#{anotherBean[field]}</h:column>
</h:dataTable>

field在哪里'user.name''location.address.zip'或...

是否可以?

请注意,这是一个简单的示例,我感兴趣的ValueExpression不是dataTable组件。

UPDATE 现在的问题是:如何替换标准的BeanELResolver?

在 ELUtils 中查找:

    ...
    composite.addRootELResolver(IMPLICIT_RESOLVER);
    composite.add(FLASH_RESOLVER);
    composite.addPropertyELResolver(COMPOSITE_COMPONENT_ATTRIBUTES_EL_RESOLVER);
    addELResolvers(composite, associate.getELResolversFromFacesConfig());
    addVariableResolvers(composite, FacesCompositeELResolver.ELResolverChainType.Faces,
            associate);
    addPropertyResolvers(composite, associate);
    composite.add(associate.getApplicationELResolvers());
    composite.addRootELResolver(MANAGED_BEAN_RESOLVER);
    composite.addPropertyELResolver(RESOURCE_RESOLVER);
    composite.addPropertyELResolver(BUNDLE_RESOLVER);
    ...

但我还没有完全理解解析器链......所以我会去学习:)

更新 2

此代码有效;)

public class ExtendedBeanELResolver extends BeanELResolver
{
    @Override
    public Object getValue(ELContext context, Object base, Object property) throws NullPointerException, PropertyNotFoundException, ELException
    {
        try
        {
            return super.getValue(context, base, property);
        }
        catch(PropertyNotFoundException e)
        {
            try
            {
                Object value = base;

                for(String part : property.toString().split("\\."))
                {
                    value = super.getValue(context, value, part);
                }

                return value;
            }
            catch(PropertyNotFoundException e1)
            {
                context.setPropertyResolved(false);
            }
        }

        return null;
    }
}
4

2 回答 2

9

默认情况下不支持此功能。你需要在ELResolver这里定制。最简单的就是扩展现有的BeanELResolver.

这是一个启动示例:

public class ExtendedBeanELResolver extends BeanELResolver {

    @Override
    public Object getValue(ELContext context, Object base, Object property)
        throws NullPointerException, PropertyNotFoundException, ELException
    {
        if (property == null || base == null || base instanceof ResourceBundle || base instanceof Map || base instanceof Collection) {
            return null;
        }

        String propertyString = property.toString();

        if (propertyString.contains(".")) {
            Object value = base;

            for (String propertyPart : propertyString.split("\\.")) {
                value = super.getValue(context, value, propertyPart);
            }

            return value;
        }
        else {
            return super.getValue(context, base, property);
        }
    }

}

要让它运行,请在以下位置注册它faces-config.xml

<application>
    <el-resolver>com.example.ExtendedBeanELResolver</el-resolver>
</application>
于 2012-05-22T04:23:12.313 回答
4

在这个主题上所做的工作非常有趣,但并不完整。

如果您在此组件中设置的复合组件中传递值,则它不起作用。

例子 :

<composite:interface>
    <composite:attribute name="value" type="java.lang.Boolean" required="true"  />
</composite:interface>
<composite:implementation>
        <h:inputText id="inputValue" value="#{cc.attrs.value}" 
        </h:inputText>
</composite:implementation>

使用 ExtendedBeanElResolver,当我设置值时它会抛出 PropertyNotFoundException。

因此,我花了几个小时来寻找解决方案,这是可行的解决方案,以便能够使用 ExtendedBeanElResolver 并能够在复合组件中设置值:

public class ExtendedBeanELResolver extends BeanELResolver {

    private static final String PRIMEFACES_RESOURCE_PREFIX = "primefaces:";
    private static final String RESOURCES_HANDLER = "class org.omnifaces.resourcehandler.GraphicResourceHandler";
    private static final String PRIMEFACES_EXT_RESOURCES_HANDLER = "class org.primefaces.extensions.application.PrimeFacesExtensionsResourceHandler";

    @Override
    public Object getValue(ELContext context, Object base, Object property) {

            if (property == null || base == null || base instanceof ResourceBundle || base instanceof Map || base instanceof Collection
                    || property.toString().startsWith(PRIMEFACES_RESOURCE_PREFIX) || base.getClass().toString().equals(RESOURCES_HANDLER)
                    || base.getClass().toString().equals(PRIMEFACES_EXT_RESOURCES_HANDLER)) {
                return null;
            }
            String propertyString = property.toString();
            if (propertyString.contains(".")) {
                Object value = base;

                for (String propertyPart : propertyString.split("\\.")) {
                    value = super.getValue(context, value, propertyPart);
                }

                return value;
            } else {
                Object v = super.getValue(context, base, property);
               return v;
            }
    }

    @Override
    public void setValue(ELContext context, Object base, Object property, Object val) {
        if (base != null && !(base instanceof ResourceBundle) && !(base instanceof Map) && !(base instanceof Collection))
            super.setValue(context, base, property, val);
    }
}

(我添加了“setValue”部分)。

现在它可以工作了。不要犹豫,给我你的反馈,因为这是我在这个网站上的第一篇文章!

于 2016-03-02T16:51:22.420 回答