【问题标题】:p:commandLink actionListener is processed for each row of p:dataTable while rendering the pagep:commandLink actionListener 在渲染页面时为 p:dataTable 的每一行处理
【发布时间】:2012-02-25 05:07:20
【问题描述】:

我有一个显示存储在数据库中的数据的数据表。其中一列包含一个 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;
}

【问题讨论】:

    标签: jsf datatable primefaces actionlistener commandlink


    【解决方案1】:

    这不是actionListener 的有效方法签名,因此&lt;p:commandLink&gt; 将其视为值表达式。

    您应该改用action

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

    请注意,您也可以只返回 void 而不是 null String

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

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

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

    另见:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      • 2020-04-18
      • 2013-08-09
      • 1970-01-01
      • 2017-06-06
      相关资源
      最近更新 更多