2

I have a problem with ActionListener. This is part of my .xhtml file with JSF2.1 dataTable component:

                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Delete"></h:outputText>
                        </f:facet>
                        <h:commandButton value="delete" action="#{productManagedBean.delete}" actionListener="#{productManagedBean.setSelectedProduct}">
                            <f:attribute name="selectedProduct" value="#{product}"></f:attribute>
                        </h:commandButton>
                    </h:column>

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

My problem is that when I use the method below as the value for dataTable:

public List<Product> getList() {
    List<Product> l = productBean.getList();
    return l;
}

it works of course, the product list shows and I can click commandButton to set the product that I want to delete and force method delete() - but when I want to show list for Customer and I use the method below where the parameter is Customer object:

public List<Product> getList() {
    List<Product> l = productBean.getList(selectedCustomer);
    return l;
}

when I use this method the list of products displays for the customer, but when I click Delete commandButton the ActionListener doesn't work.

Backing Bean:

@Named
@RequestScoped
public class ProductManagedBean {

private Product selectedProduct = new Product();
private Customer selectedCustomer = new Customer();

@EJB
ProductBeanLocal productBean;

public List<Product> getList() {
    //List<Product> l = productBean.getList(selectedCustomer); -- PROBLEM
    List<Product> l = productBean.getList();
    return l;
}

//listener
public void setSelectedProduct(ActionEvent e) {
    selectedProduct = (Product) e.getComponent().getAttributes().get("selectedProduct");
}

EJB Component:

@Stateless
public class ProductBean implements ProductBeanLocal {

@PersistenceContext(unitName = "CustomerServicePU")
EntityManager em;

@Override
public List<Product> getList() {
    List<Product> list = em.createQuery("SELECT p FROM Product p").getResultList();
    return list;
}

@Override
public List<Product> getList(Customer c) {
    List<Product> list = em.createQuery("SELECT p FROM Product p WHERE p.customer.id = :id").setParameter("id", c.getId()).getResultList();
    return list;
}

Does anyone have any idea why it does not work?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • `selectedCustomer` is never set. i can not see. however, you try to get some results based on `selectedCustomer`. – erencan Jul 17 '13 at 06:50
  • [Just stop doing business logic in getter methods](http://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times/2090062#2090062)! They are the **wrong** place to perform business logic. Your concrete problem is duplicated and answered in point 4 of [h:commandLink/h:commandButton is not being invoked](http://stackoverflow.com/questions/2118656/hcommandlink-hcommandbutton-is-not-being-invoked/2120183#2120183). – BalusC Jul 17 '13 at 13:52

0 Answers0