嵌套JSF表达式字符串 [英] nesting JSF expression strings

查看:144
本文介绍了嵌套JSF表达式字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用值表达式的动态参数实现此目的:

I want to achieve this using dynamic parameters for value expressions:

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

其中字段'user.name''location.address.zip'或...

有可能吗?

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

Note that this is a simple example, I'm interested in ValueExpression not in dataTable component.

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

UPDATE now the question is: how to replace standard 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);
    ...

但是我还没有完全理解旋转变压器链......所以我将继续学习:)

but i don't fully understand resolver chain yet... so i'll go studying :)

更新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;
    }
}


推荐答案

这默认情况下不受支持。您需要一个自定义的 ELResolver 。最简单的是扩展现有的 BeanELResolver

This is by default not supported. You need a custom ELResolver here. Easiest is to extend the existing 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 中注册如下:

To get it to run, register it as follows in faces-config.xml:

<application>
    <el-resolver>com.example.ExtendedBeanELResolver</el-resolver>
</application>

这篇关于嵌套JSF表达式字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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