Jsf p内的commandButton:数据表不工作

Jsf p内的commandButton:数据表不工作,jsf,primefaces,jsf-2,Jsf,Primefaces,Jsf 2,我知道这里也有类似的问题,但没有一个解决方案适合我。在我使用primeface数据表之前,每件事情都运行得很好,但我需要primefaces的分页器,所以我切换到primeface的数据表,而不是默认的jsf数据表。问题是我有一个名为action的列,其中将放置一些按钮。例如,其中一个按钮是“购买”。当我点击这个按钮时,程序将从产品数据库中获取并将其添加到购物车数据库中。这在jsf datatable中工作得非常好。但在primefaces数据表中,它要么不做任何事情,要么给出空点异常。这是我的

我知道这里也有类似的问题,但没有一个解决方案适合我。在我使用primeface数据表之前,每件事情都运行得很好,但我需要primefaces的分页器,所以我切换到primeface的数据表,而不是默认的jsf数据表。问题是我有一个名为action的列,其中将放置一些按钮。例如,其中一个按钮是“购买”。当我点击这个按钮时,程序将从产品数据库中获取并将其添加到购物车数据库中。这在jsf datatable中工作得非常好。但在primefaces数据表中,它要么不做任何事情,要么给出空点异常。这是我的xhtml文件。如果我单击datatable中第一个项目的buy按钮,它会给出空点异常。如果我单击其他任何项目,它不会做任何事情

<?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();
}
我真的很感激任何帮助。
谢谢。

我找到了解决办法。我需要从action列的code block中删除标记,它就可以工作了。

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