2017-06-13 86 views
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(); 
} 

我真的很感激任何帮助。 谢谢。

回答

0

我找到了解决方案。我需要从代码块的行列中删除标签<h:form></h:form>,它工作。

+1

jfym:你不应该使用嵌套窗体!如果你在其他网站上也使用嵌套表单,你应该改变它,因为它总是会导致这样的错误。 – lastresort