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?