使用 RichFaces 的 ModalPanel

ModalPanel using RichFaces

我正在尝试从数据表(RichFaces 4.5.1、MyFaces 2.2.6、Spring 3.1.1、Tomcat 7.0.27)创建一个 ModalPanel,但我做不到.

modalPanel有值根据table中选择的行显示。当我在 commandLink 中单击时,触发 actionListener 应该提供这些值 (localidadeMB.carregarColecoes()),但它不起作用,因为对 bean 属性的引用为空(尚未由 f:setPropertyActionListener 设置位于 commandLink)。

缺少了什么?

*page.xhtml

(...)
<f:view>
<h:form id="formConsulta">
    <rich:dataTable id="tabela"
        value="#{localidadeMB.getLocalidadesPorPalavra()}" var="loc"
        rowKeyVar="row" rows="20" width="800px" render="scroller">
        <rich:column>
            (...)
        </rich:column>
        <rich:column>
            <f:facet name="header">
                <h:outputText value=""/>
            </f:facet>
            <a4j:commandLink id="editlink" ajaxSingle="true" 
                oncomplete="#rich:component('modalEditarLocalidade')}.show()"
                actionListener="#{localidadeMB.carregarColecoes}" 
                render="modalEditarLocalidade">
            <h:graphicImage value="/img/edit.png"/>
            <f:setPropertyActionListener value="#{loc}" target="#{localidadeMB.localidade}" />
            </a4j:commandLink>
        </rich:column>
    </rich:dataTable>
</h:form>

<ui:include src="modalPanel.xhtml" />

*modalPanel.xhtml

<ui:composition>
<rich:popupPanel id="modalEditarLocalidade" autosized="true"
    modal="false" resizable="false">
    <f:facet name="header">
        <h:outputText value="Alterar localidade" />
    </f:facet>
    <f:facet name="controls">
        <h:graphicImage id="modalClosePNG2" value="/img/close1.png"
            style="cursor:default;"
            onclick="Richfaces.hideModalPanel('modalEditarLocalidade')" />
    </f:facet>
    <h:form>
        <h:outputLabel value="Nome da localidade:" for="nomeLocalidade" />
        <h:inputText id="nomeLocalidade"
            value="#{localidadeMB.localidade.nome}" required="true"
            immediate="true"/>
        </h:inputText>
    </h:form>
</rich:popupPanel>
</ui:composition>

*ManagedBean

private Localidade localidade;

public void setLocalidade(Localidade l){
    this.localidade = l;
}

public void carregarColecoes(ActionEvent action){
    System.out.println(localidade.getNome());

        ***** print NULL *****
}

如果你想先设置一个变量然后执行一个方法,你必须为该方法使用@action,首先执行actionListeners但是因为你的两个方法都是actionListeners所以localidadeMB.carregarColecoes先执行变量已设置。 (顺便说一句。h:commandLink 没有 "ajaxSingle" 或 "oncomplete" 属性。)

在 Makhiel 和 this link 的建议下,我决定了。

xhtml 看起来像这样:

    <a4j:commandLink id="editlink" ajaxSingle="true"
        oncomplete="#{rich:component('modalEditarLocalidade')}.show()"
        action="#{localidadeMB.carregarColecoes}" 
        render="modalEditarLocalidade">
        <h:graphicImage value="/img/edit.png"/>
        <f:setPropertyActionListener value="#{loc}"
            target="#{localidadeMB.localidade}" />
    </a4j:commandLink>

ManagedBean 是这样的:

    public String carregarColecoes(){
        (....)
    }