8

我有以下两个简单的 POJO:

class Person {
   String name
   Address address;
   //and of course the getter/setter for the attributes
}

class Address {
   String city;
   //also getter/setter for this attribute
}

还有一个支持bean:

@ManagedBean
@RequestScoped
class PersonController {

    private List persons;
    private List<String> columns = Arrays.toList("name", "address.city");
   //of course getter/setter
}

现在我想创建一个数据表。

<p:dataTable var="person" value="#{personController.persons}" columnIndexVar="index">
    <p:columns var="column" value="#{personController.columns}">
        <h:outputText value="#{person[column]}"/>
    <p:columms>
</p:dataTable>

当我执行这个时,我得到一个 ServletException:

Person 类没有属性“address.city”。

但是,如果尝试在 p:columns 中访问这样的属性城市:

<h:outputText value="#{person.address.city}"/>

一切安好。

为什么我不能访问这样的嵌套属性#{person['address.city']}?我怎样才能在里面访问它p:columns

4

2 回答 2

9

#{person['address.city']}默认情况下,不支持大括号表示字符串表达式中的嵌套 bean 属性。你基本上需要一个#{person['address']['city']}.

你需要在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-23T14:51:19.940 回答
1

除了@BalusC 答案之外,我还必须添加对 PrimeResourceHandler 的检查。否则,所有 #{resource...} 解析(如 primefaces.css 中的 #{resource['primefaces:spacer/dot_clear.gif']} )都会失败,解析后的 CSS 文件的输出流会损坏。

public class ExtendedBeanELResolver extends BeanELResolver {

    private static final String PRIMEFACES_RESOURCE_PREFIX = "primefaces:";

    @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 || base instanceof PrimeResourceHandler) {
            return null;
        }

        String propertyString = property.toString();

        if (!propertyString.startsWith(PRIMEFACES_RESOURCE_PREFIX) && 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);
        }
    }
}
于 2014-01-28T06:34:54.683 回答