我正在尝试使用primeface数据表进行基本的添加和删除数据库操作。添加工作正常,但无法使删除工作。当我单击删除按钮时,不会调用删除功能。
<h:body>
<h:form>
<p:panel id="toppanel" closable="true" toggleable="true" >
<f:facet name="Store">
<p:outputLabel value="Header" />
</f:facet>
<f:facet name="@Copyright" >
<p:outputLabel value="footer" />
</f:facet>
<p align="left" >
<p:inputText value="#{salesPerson.item.itemId}" id="itemid" >
<f:passThroughAttribute name="placeholder" value="ID"/>
</p:inputText>
<p:inputText value="#{salesPerson.item.itemTitle}" >
<f:passThroughAttribute name="placeholder" value="TITLE"/>
</p:inputText>
<p:inputText value="#{salesPerson.item.itemDesc}" >
<f:passThroughAttribute name="placeholder" value="DESCRIPTION"/>
</p:inputText>
<p:inputText value="#{salesPerson.item.price}" >
<f:passThroughAttribute name="placeholder" value="PRICE"/>
</p:inputText>
</p>
<p align="right" >
<p:commandButton process="@form" id="Create" value="Create" update="out" actionListener="#{salesPerson.addItem()}" />
</p>
<p:dataTable id="out" var="item" value="#{salesPerson.items}" rowKey="#{item.itemId}" selection="#{salesPerson.selectedItems}" >
<p:column selectionMode="multiple" width="1%"/>
<p:column>
<f:facet name="header">
<h:outputText value="ID" />
</f:facet >
<h:outputText value="#{item.itemId}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="TITLE" />
</f:facet >
<h:outputText value="#{item.itemTitle}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="DESCRIPTION" />
</f:facet >
<h:outputText value="#{item.itemDesc}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="PRICE" />
</f:facet >
<h:outputText value="#{item.price}" />
</p:column>
</p:dataTable>
<p align="right" >
<p:commandButton process="@form" id="delete" value="Delete" update="out" actionListener="#{salesPerson.deleteItem(items)}" />
</p>
</p:panel>
</h:form>
</h:body>
</html>
SalesPerson Bean 这是控制器支持bean
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.parthi.controller;
import javax.inject.Inject;
import com.parthi.model.Item;
import com.parthi.backing.ShoppingBean;
import com.parthi.service.StoreKeeper;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
@RequestScoped
@Named
public class SalesPerson implements Serializable{
@Inject
private Item item;
private List<Item> items = new LinkedList<>();
private List<Item> selectedItems = new LinkedList<>();
@EJB
private StoreKeeper storeKeeper;
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
public List<Item> getSelectedItems() {
System.out.println("Number of items getselected" +selectedItems.size());
return selectedItems;
}
public void setSelectedItems(List<Item> selectedItems) {
System.out.println("Number of items setselected" +selectedItems.size());
this.selectedItems = selectedItems;
}
public void addItem(){
storeKeeper.addItem(item);
// System.out.println("Printing add item");
}
public List<Item> getAllItems() {
// System.out.println("Getting all SalesPerson-items");
items = storeKeeper.getAllItems();
return items;
}
public void deleteItem(List<Item> items){
System.out.println("Reached SalesPerson.deleteItem");
int i =0;
while(i <= items.size()){
storeKeeper.deleteItem(items.get(i));
i++;
System.out.println("deleting..." + items.get(i));
}
}
public List<Item> getItems() {
getAllItems();
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}
服务EJB Service是一个执行CRUD操作的无状态会话bean
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.parthi.service;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import com.parthi.model.Customer;
import com.parthi.model.Item;
@Stateless
public class StoreKeeper {
@PersistenceContext
private EntityManager em;
public List<Item> getAllItems(){
System.out.println("Getting all StoreKeeper-item");
return em.createQuery("FROM Item p", Item.class).getResultList();
}
public void addItem(Item item){
System.out.println("Printing all StoreKeeper add item");
em.persist(item);
}
public void deleteItem(Item item){
em.merge(item);
em.remove(item);
}
}