0
我知道這裏有類似的問題,但沒有解決方案爲我工作。在我使用primeface數據表之前,每件事情都很完美,但我需要primefaces的paginator,所以我切換到primeface的數據表而不是默認的jsf數據表。問題是我有一個名爲action的列,其中somebuttons將被放置。例如,其中一個按鈕是「購買」。當我點擊這個按鈕時,程序將從產品數據庫中取出並添加到購物車數據庫。這與jsf datatable完美配合。但在primefaces數據表中,它要麼沒有做任何事情,要麼給出零點異常。這是我的xhtml文件。如果點擊數據表第一項的購買按鈕(如果我點擊其他任何內容而沒有執行任何操作),它會給出零點例外。命令按鈕內部p:datatable不工作
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Shopping</title>
<h:outputStylesheet library="css" name="Product-table.css" />
</h:head>
<body>
<h:outputLabel value="Welcome #{loginBean.name}"></h:outputLabel>
<br></br>
<br></br>
<div style="width: 100%;">
<div style="width: 75%; float: left;">
<h2>Products</h2>
<h:form>
<p:dataTable var="product" value="#{databaseBean.products}"
filteredValue="#{databaseBean.filteredProducts}"
widgetVar="productWidget" paginator="true" rows="10"
paginatorTemplate="{CurrentPageReport} {FirstPageLink}
{PreviousPageLink} {PageLinks} {NextPageLink}
{LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15,20,25">
<f:facet name="header">
<p:outputPanel>
<h:outputText value="Search:" />
<p:inputText id="globalFilter"
onkeyup="PF('productWidget').filter()" style="width:150px"
placeholder="Search text" />
</p:outputPanel>
</f:facet>
<p:column headerText="Id" filterBy="#{product.id}" filterMatchMode="contains">
<h:outputText value="#{product.id}" />
</p:column>
<p:column headerText="Name" filterBy="#{product.name}" filterMatchMode="contains" >
<h:outputText value="#{product.name}" />
</p:column>
<p:column headerText="Price" filterBy="#{product.price}" filterMatchMode="contains">
<h:outputText value="#{product.price}" />
</p:column>
<p:column headerText="Quantity" filterBy="#{product.quantity}" filterMatchMode="contains">
<h:outputText value="#{product.quantity}" />
</p:column>
<p:column headerText="Category" filterBy="#{product.category}" filterMatchMode="contains" >
<h:outputText value="#{product.category}" />
</p:column>
<p:column>
<f:facet name="header">Action</f:facet>
<h:form>
<h:commandButton type="submit" value="#{product.id}"
name="buyLink" action="#{databaseBean.addtoCart}">
<f:param name="productId" value="#{product.id}" />
<f:param name="userId" value="#{loginBean.name}" />
</h:commandButton>
</h:form>
</p:column>
</p:dataTable>
</h:form>
</div>
<div style="width: 25%; float: right;">
<h2>Cart</h2>
<p:dataTable value="#{databaseBean.cart}" var="product"
styleClass="product-table" headerClass="product-table-header"
rowClasses="product-table-odd-row,product-table-even-row">
<p:column>
<f:facet name="header">Product ID</f:facet>
#{product.productId}
</p:column>
<p:column>
<f:facet name="header">Quantity</f:facet>
#{product.quantity}
</p:column>
</p:dataTable>
</div>
</div>
<h:form>
<h:commandButton action="#{loginBean.logout}" value="logout"></h:commandButton>
</h:form>
這是按鈕應調用該方法。正如我所說的,它在默認的jsf數據表中完美工作,所以我認爲java代碼沒有什麼問題,但問題出在xhtml文件中。
public void addtoCart(){
//userId = getUserID();
ManageCart MC = new ManageCart();
FacesContext fc = FacesContext.getCurrentInstance();
Map<String,String> params =
fc.getExternalContext().getRequestParameterMap();
String productIdString = params.get("productId");
int productId = Integer.parseInt(productIdString);
MC.addToChart(userId, productId, 1);
cart = mc.listCart(userId);
products = mp.listProducts();
}
我真的很感激任何幫助。 謝謝。
jfym:你不應該使用嵌套窗體!如果你在其他網站上也使用嵌套表單,你應該改變它,因爲它總是會導致這樣的錯誤。 – lastresort