English 中文(简体)
在 p: cluunts 中无法访问管理豆类中的嵌套属性
原标题:No access to nested property in managed bean within p:columns

我有两个简单的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
}

和一个支持豆:

@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>

当我执行这个任务时,我得到一个服务例外:

个人没有产权地址.城市。

但如果试图在p: columens 内进入这样的地产城市:

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

一切都没事。

为什么我不能访问这样的嵌套属性? {{{{{{{{{{{{{{{}}}}}{}}{}}{}}{}}{}}{}}{}}{}}{}}{}}{}}}{}}{}}{}}}{}}{}}{}{}}}{}}}{}}{}}{}{}{}{}{}{}{}{}{}{}{}{}{{}{}{}{}{}{}{}{}{}{}{}{}}}{}}{{}}}}我怎能在 {{{{{{{{{{{{{{{{{{{{{{}}{{{{{}}}}内访问它?{{{}}}}}{{{{}}}}}}{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{}}}}}}}}

最佳回答

括号符号字符串表达式中如

您需要在此自定义 < code> ELResolver 。 最容易的是扩展现有的 < a href=" http://docs. oracle. com/javaee/6/ api/javae/6/ javax/ el/ e/ BeanELResolver. html" \ code > 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>
问题回答

除了@BalusC 回答之外, 我还必须为 PrimeResourcesHandler 添加一张支票。 否则, 资源的所有解析...} 如 资源[ 原始面 : spacer/ dot_ clear. gif] 。 cs 失败, 解析的 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);
        }
    }
}




相关问题
JSF a4j:support with h:selectManyCheckbox

I m having trouble with a JSF selectManyCheckbox and A4J support. The purpose is to run some action when a checkbox is selected. This works perfectly in Firefox. Yet, when testing in any IE (ie6 / ie7 ...

Mojarra for JSF Encoding

Can anyone teach me how to use mojarra to encode my JSF files. I downloaded mojarra and expected some kind of jar but what i had downloaded was a folder of files i don t know what to do with

如何拦截要求终止?

在共同基金中,如果用户要求终止,就需要采取一些行动。 我需要某种拦截器,但我不知道如何这样做。 我需要帮助。 增 编

ICEFaces inputFile getting the file content without upload

Is there any way of just getting the content of the browsed file without any upload/file transfer operations? I currently use ICEFaces inputFile component but I do not need the default uploading ...

Weird behaviour of h:commandLink action (MethodExpression)

I have two JSPs where I am displaying some info from database in a h:dataTable. One of them is showing all the info, and one of them user specifically. I have showXML.jsp that shows the "XML" column ...

How to correctly use ResultSet with h:dataTable

The problem is, that after displaying the ResultSet with <h:dataTable>, the connection is left open. If I close it, it closes the ResultSet too. I m thinking about copying the ResultSet data ...

热门标签