0

我有一个显示存储在数据库中的数据的数据表。其中一列包含一个commandLink(p:commandLink)来编辑选定的行,这很好。

我在渲染我的 xhtml 页面时遇到了问题。似乎commandLink的actionListener属性中backingBean的方法是针对表中的每一行处理的,但是actionListener应该只有在点击链接时才处理。

这是我的 xhtml 页面(部分):

<h:form id="formElenco">
    <p:dataTable id="dt1" value="#{myBean.itemList}" var="item">
        <f:facet name="header">
            <h:outputText value="header" />
        </f:facet>
        <p:column>
            <f:facet name="header">
                <h:outputText value="Name" />
            </f:facet>
            <h:outputText value="#{item.name}"/>
        </p:column>
        <p:column>
            <f:facet name="header">
                <h:outputText value="value" />
            </f:facet>
            <h:outputText value="#{item.value}"/>
        </p:column>
        <p:column>
            <p:commandLink id="lnkItemUpdate" value="Edit"
                            onstart="document.getElementById('divUpdateItem').style.display = 'block';"
                            actionListener="#{myBean.updateItemAction(item)}" update=":formUpdateItem" />
        </p:column>

    </p:dataTable>
</h:form>

<div id="divUpdateItem" style="display:none" > 
    <h:form id="formUpdateItem">
        Nome <h:inputText value="#{myBean.name}" /><br/>
        Met  <h:inputText value="#{myBean.value}" /><br/>
        <h:inputHidden value="#{myBean.id}" />
        <h:commandButton action="#{myBean.updateItemAction}" value="Save" />
    </h:form>
</div>

下面是 myBean 的方法(myBean 是 requestScoped):

public String updateItemAction(Entity item){
    this.setId(item.getId());
    this.setName(item.getName());
    this.setValue(item.getValue());
    return null;
}

public String updateItemAction() {
    Entity entity = new Entity();
    entity.setId(this.getId());
    entity.setName(this.getName());
    entity.setVAlue(this.getValue());
    updateEntityQueryMethod(entity);
    return null;
}
4

1 回答 1

2

这不是 的有效方法签名actionListener,因此它被<p:commandLink>.

你应该action改用。

<p:commandLink id="lnkItemUpdate" value="Edit"
    onstart="document.getElementById('divUpdateItem').style.display = 'block';"
    action="#{myBean.updateItemAction(item)}" update=":formUpdateItem" />

请注意,您也可以只 returnvoid而不是 a null String

public void updateItemAction(Entity item) {
    this.setId(item.getId());
    this.setName(item.getName());
    this.setValue(item.getValue());
}

一个有效的actionListener方法签名将是一个void接受javax.faces.event.ActionEvent参数的方法:

public void actionListener(ActionEvent event) {
    // ...
}

也可以看看:

于 2012-02-02T14:52:52.627 回答