سؤال

I want to put a h:commandLink in a h:dataTable to delete a single row. Here I have a code which does not work and I hope you can help me. This code doesn't even call my delete method. The data for the h:dataTable comes from a database.

XHTML

        <h:form>
            <div class="center">
                <h:dataTable id="list" value="#{firstBackingBean.list}"
                    var="first" styleClass="center">

                    <!-- other columns (take data from firstBackingBean) -->

                    <h:column>
                        <f:facet name="header" />
                        <h:commandLink value="delete" action="#{secondBackingBean.delete}" >
                            <f:ajax execute="@form" render="list" />
                            <f:param name="id" value="#{first.id}"/>
                        </h:commandLink>
                    </h:column>
                </h:dataTable>
            </div>
        </h:form>

FirstBackingBean

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class FirstBackingBean {
    public void delete() {
        System.out.println("\nBUTTON CLICKED\n");
    }
}

SecondBackingBean

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class SecondBackingBean {
    // ...
}
هل كانت مفيدة؟

المحلول 2

I've the answer!

I've 2 BackingBeans, the firstBackingBean needs the @SessionScoped annotation and the secondBackingBean needs the @ViewScoped annotation.

Thanks a lot for the other answers.

نصائح أخرى

When an UICommand component inside an UIData component doesn't invoke its action, then it often means that the model behind the value attribute of the UIData component has incompatibly changed between the request of displaying the form with the table and the subsequent request of submitting the form.

This can happen if the backing bean is request scoped and the model is preinitialized based on a request scoped variable (such as a request parameter), which isn't present during the form submit request anymore.

Placing the bean in the view scope should fix it.

See also:

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top