【问题标题】:PrimeFaces p:ajax Unable to resolve beanPrimeFaces p:ajax 无法解析 bean
【发布时间】:2014-08-25 21:42:01
【问题描述】:

我正在使用 PrimeFaces 5 编写一个应用程序,目前我正在编写一个包含几个 AutoComplete 元素的屏幕,其想法是一个自动完成的值会影响第二个自动完成中可用的选择。

我试图让它工作的方式是,当它填充第一个自动完成时,会触发一个 Ajax 事件以使用该值更新支持 bean,因此当调用第二个自动完成时,它具有该值从第一个自动完成准备开启。

这是页面:

<ui:composition 
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://xmlns.jcp.org/jsf/passthrough"
    xmlns:pf="http://primefaces.org/ui">

    <pf:panel id="header" header="Details" style="margin-bottom:20px">
        <h:panelGrid columns="3" style="margin-bottom:10px;border-style:solid;border-width:0.1em;width:100%" cellpadding="5">
            <pf:outputLabel id="input1Label" for="input1" value="Specify Input 1" />
            <pf:autoComplete id="input1" value="#{bean.input1}" completeMethod="#{bean.completeInput1}"
                        var="input1" itemLabel="#{input1}" itemValue="#{input1}" >
                <pf:ajax event="itemSelect" listener="#{bean.input1ItemSelect}"/>
            </pf:autoComplete>
            <pf:message id="input1Msg" for="input1"/>

            <pf:outputLabel id="input2Label" for="input2" value="Input 2?" />
            <pf:autoComplete id="input2" value="#{bean.input2}" completeMethod="#{bean.completeInput2}"
                        var="input2" itemLabel="#{input2}" itemValue="#{input2}" />
            <pf:message id="input2Msg" for="input2"/>
        </h:panelGrid>
    </pf:panel>

</ui:composition>

但是,当在 input1 中选择一个值时,我收到此错误:

无法访问,标识符“bean”解析为 null:javax.el.PropertyNotFoundException:目标无法访问,标识符“bean”解析为 null

接下来要指出的是,这个组合包含在父组合中,并且“bean”作为参数以下列方式传入:

<c:forEach items="#{parentBean.blockNames}" var="blockName" varStatus="loop">

    <f:subview id="block_subview_#{loop.index}">
        <ui:include src="#{blockName}">
            <ui:param name="bean" value="#{parentBean.blockBeans[loop.index]}"/>
        </ui:include>
    </f:subview>
</c:forEach>

我能够确定的是,如果我将 pf:ajax 标记中的引用“bean”更改为命名 bean,它能够解析 bean 引用并尝试调用侦听器方法。因此,出于某种原因,pf:ajax 标记似乎无法处理“bean”引用,因为它不是实际 bean 的名称,而是从父级传入的 bean 参数的名称。然而,所有其他标签都可以完美地解析这里的“bean”。

有没有办法解决这个问题?

编辑:

将 pf:ajax 更改为 f:ajax 似乎使错误消失,至少随后调用了正确的侦听器方法。我宁愿不使用这种方法,但也许这些信息很有用。

【问题讨论】:

    标签: jsf jsf-2 primefaces


    【解决方案1】:

    您可以尝试将包含页面的内容放在composite component 中,然后使用composite component 而不是ui:include

    我曾在几个复杂的 JSF 网站上工作,从未遇到过 composite component 技术的任何问题,而那些 ui:include 总是在循环中产生某种问题。

    【讨论】:

      【解决方案2】:

      找到原因,原来支持 bean(或者具体来说,父页面的支持 bean)正在使用 @RequestScoped(或者更确切地说,它默认为 this,因为没有指定范围),所以大概不再存在用户调用它的 ajax 调用的时间。将其更改为 @ViewScoped 可以解决问题。

      【讨论】:

        【解决方案3】:

        嗯,

        这可能是由于 c:forEach 标签造成的。尝试改用 ui:repeat

        c:forEach 不是 JSF 标签,可能会造成混淆。

        【讨论】:

        • 不幸的是,当我使用 时,在父页面中动态包含子视图的设计不起作用,根据我的阅读 是使此行为起作用所必需的,例如此线程上的答案:stackoverflow.com/questions/20111010/…
        • 也许你可以在this answer找到有用的东西。
        • 这是我的答案 :-) 尽管正如我提到的那样,该方法拒绝适用于我需要的设计,所以我不得不稍后根据我链接到的线程恢复到 c:forEach。跨度>
        猜你喜欢
        • 2011-08-20
        • 1970-01-01
        • 1970-01-01
        • 2012-05-28
        • 2015-05-02
        • 2014-08-01
        • 2015-02-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多