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

查看:16
本文介绍了嵌套 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>

其中 field'user.name''location.address.zip' 或...

where field is 'user.name' or 'location.address.zip' or...

有可能吗?

注意,这是一个简单的例子,我对 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 中查找:

looking in 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天全站免登陆