我正在使用一个包含购物车的JSF小型Web应用程序,用户可以添加项目,然后转到其购物车中查看它们,这里提供了删除项目的选项。最后,用户可以返回到商品目录或完成订单。
问题是单击“删除”按钮后没有任何反应。它应该从表中删除行信息,负责执行此任务的控制器和模型可以正常工作,按钮是不执行代码的按钮。
这是视图:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://xmlns.jcp.org/jsf/passthrough"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:x="http://primefaces.org/ui">
<ui:composition template="/WEB-INF/templates/page.xhtml">
<ui:define name="main">
<h:form prependId="false">
<table>
<tr>
<th>Delete</th>
<th>Item</th>
<th>Price</th>
<th>Quantity</th>
<th>Sub Total</th>
</tr>
<ui:repeat var="item" value="#{orderItemCtrl.getOrderItemsInCart(orderCtrl.getcartId())}">
<tr>
<td>
<h:form prependId="false">
<h:commandLink value="Delete" action="#{orderItemCtrl.destroy(b.ID)}">
</h:commandLink>
</h:form>
</td>
<td>#{itemCtrl.items[item.itemId - 1].title}</td>
<td>#{itemCtrl.items[item.itemId - 1].price}</td>
<td>#{item.quantity}</td>
<td>#{itemCtrl.items[item.itemId - 1].price * item.quantity}</td>
</tr>
</ui:repeat>
<tr>
<td>Total</td>
<td>Order Total: $#{orderCtrl.getOrder(orderCtrl.getcartId()).price}</td>
</tr>
</table>
<h:commandLink value="Finish Order" action="#{orderCtrl.finishOrder(orderCtrl.getcartId())}"></h:commandLink>
</h:form>
<h:button value="Back to Catalog" class="btn btn-lg btn-info" outcome="catalog" />
</ui:define>
</ui:composition>
</html>
orderItemController中的“销毁”方法:
public void destroy(int id) {
try {
System.out.println("Destroying orderItem with id: " + id);
OrderItem.destroy( em,ut,id);
} catch ( Exception e) {
e.printStackTrace();
}
}
最后是模型中的“销毁”方法:
public static void destroy(EntityManager em,UserTransaction ut,int orderItemID) throws Exception {
ut.begin();
OrderItem orderItem = em.find(OrderItem.class,orderItemID);
em.remove(orderItem);
ut.commit();
System.out.println( "OrderItem.destroy: the orderItem " + orderItem + " was deleted.");
}
它确实可以正确渲染网站,并且“完成订单”和“返回目录”按钮可以正常工作。