单击commandbutton时获取NULL值

时间:2014-08-05 04:38:42

标签: java mysql primefaces managed-bean

单击commandButton ipsDetail值时,manageBean中的所有值都为NULL。在相同的页面中我得到的其他表格数据但是在关注的数据表之后,数据表无法正常工作。

XHTML文件:

<p:dataTable id="invoiceTable" var="ipsDetail"
        value="#{invoiceBean.ipsDetails}" border="1" editable="true">


        <p:column headerText="Sr. No.">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{ipsDetail.serialNumber}" />
                </f:facet>
                <f:facet name="input">
                    <h:inputText value="#{ipsDetail.serialNumber}" size="3" />
                </f:facet>
            </p:cellEditor>
        </p:column>

        <p:column headerText="Description of Goods">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{ipsDetail.descriptionOfGoodsOrService}" />
                </f:facet>
                <f:facet name="input">
                    <h:inputTextarea value="#{ipsDetail.descriptionOfGoodsOrService}" />
                </f:facet>
            </p:cellEditor>
        </p:column>

        <p:column headerText="HSN Code">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{ipsDetail.hsnCode}" />
                </f:facet>
                <f:facet name="input">
                    <h:inputText value="#{ipsDetail.hsnCode}" />
                </f:facet>
            </p:cellEditor>
        </p:column>

        <p:column headerText="Quantity">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{ipsDetail.quantity}" />
                </f:facet>
                <f:facet name="input">
                    <h:inputText value="#{ipsDetail.quantity}" size="3" />
                </f:facet>
            </p:cellEditor>
        </p:column>

        <p:column headerText="Rate">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{ipsDetail.rate}" />
                </f:facet>
                <f:facet name="input">
                    <h:inputText value="#{ipsDetail.rate}" size="3" />
                </f:facet>
            </p:cellEditor>
        </p:column>

        <p:column headerText="Percentage Discount">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{ipsDetail.percentDiscount}" />
                </f:facet>
                <f:facet name="input">
                    <h:inputText value="#{ipsDetail.percentDiscount}" size="2" />
                </f:facet>
            </p:cellEditor>
        </p:column>

        <p:column headerText="Amount">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{invoiceBean.amount}" />
                </f:facet>
                <f:facet name="input">
                    <h:inputText value="#{invoiceBean.amount}" />
                </f:facet>
            </p:cellEditor>
        </p:column>

        <p:column>
            <p:rowEditor />
        </p:column>


        <f:facet name="footer">
            <p:commandButton value="Add New Row"
                actionListener="#{invoiceBean.addRow}" update="invoiceTable"
                process="@this invoiceTable"
                oncomplete="jQuery('.ui-datatable-data tr').last().find('span.ui-icon-pencil').each(function(){jQuery(this).click()});" />
        </f:facet>

    </p:dataTable>
 <p:commandButton value="Add Invoice"
        action="#{invoiceBean.getInvoiceData()}" ajax="false" />

Manage Beans:invoiceBean它存储所有ipsDetail。

@ManagedBean(name = "invoiceBean", eager = true)
@ViewScoped
public class InvoiceBean implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private String invoiceNumber;
private Date date;
private String deliveryNote;
private String modeOfPayment;
private int supplierReference;
private String otherReference;
private String buyerName;
private Date dispatchDocumentDate;
private String dispatchThrough;
private String destination;
private String email;
private String termsOfdelivery;
private String buyerOrderNumber;
private int amount;

private int serialNumber;
private String descriptionOfGoodsOrService;
private String hsnCode;
private int quantity;
private int rate;
private int percentDiscount;
private float vat;
private float serviceTax;

private int total;

private List<Invoice> invoices;
private List<InvoiceProductsServicesDetail> ipsDetails;
private Invoice invoice;
private InvoiceProductsServicesDetail ipsDetail;


public List<Invoice> getInvoices() {

    InvoiceDao invoiceDao = new InvoiceDao();
    invoices = invoiceDao.getInvoiceData();

    return invoices;
}


public void setInvoices(List<Invoice> invoices) {
    if (invoices != null) {
        this.invoices.add(new Invoice());
    }
}

public void saveInvoiceData() {
    InvoiceDao invoiceDao = new InvoiceDao();
    invoice = new Invoice();
    ipsDetail = new InvoiceProductsServicesDetail();
    try {
        if (invoice != null && ipsDetail != null) {
            invoice.setInvoiceNumber(getInvoiceNumber());
            invoice.setDate(date);
            invoice.setDeliveryNote(deliveryNote);
            invoice.setModeOfPayment(modeOfPayment);
            invoice.setSupplierReference(supplierReference);
            invoice.setOtherReference(otherReference);
            invoice.setBuyerName(buyerName);
            invoice.setBuyerOrderNumber(buyerOrderNumber);
            invoice.setOrderDate(new Date());
            invoice.setDispatchDocumentDate(dispatchDocumentDate);
            invoice.setDispatchThrough(dispatchThrough);
            invoice.setDestination(destination);
            invoice.setTermsOfdelivery(termsOfdelivery);
            invoice.setEmail(email);
            invoice.setTotal(total);
            // invoiceDao.insertInvoice(invoice);

            System.out.println(getAmount());

            ipsDetail
                    .setDescriptionOfGoodsOrService(getDescriptionOfGoodsOrService());
            ipsDetail.setHsnCode(hsnCode);
            ipsDetail.setInvoiceId(invoice.getId());
            ipsDetail.setPercentDiscount(percentDiscount);
            ipsDetail.setQuantity(quantity);
            ipsDetail.setRate(rate);
            ipsDetail.setSerialNumber(serialNumber);
            ipsDetail.setServiceTax((float) 12.36);
            ipsDetail.setVat(5);
            invoiceDao.insertIpsDetail(ipsDetail);

            System.out.println("InvoiceBean.insertInvoice");
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}

public void addRow(){

    ipsDetail = new InvoiceProductsServicesDetail();
    ipsDetails.add(ipsDetail);
    FacesContext facesContext = FacesContext.getCurrentInstance();
    try {

        DataTable table = (DataTable) facesContext.getViewRoot().findComponent("form:invoiceTable");

        UIComponent uiTable = ComponentUtils.findParentForm(facesContext, table);

        final AjaxBehavior behavior = new AjaxBehavior();
        RowEditEvent rowEditEvent = new RowEditEvent(uiTable, behavior, table.getRowData());


        rowEditEvent.setPhaseId(PhaseId.UPDATE_MODEL_VALUES);
        table.broadcast(rowEditEvent);


    } catch (AbortProcessingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public InvoiceBean() {
    ipsDetails = new ArrayList<InvoiceProductsServicesDetail>();
}


public InvoiceProductsServicesDetail getIpsDetail() {
    return ipsDetail;
}

public void setIpsDetail(InvoiceProductsServicesDetail ipsDetail) {
    this.ipsDetail = ipsDetail;
}

public void setIpsDetails(List<InvoiceProductsServicesDetail> ipsDetails) {
    if(ipsDetails != null){
        this.ipsDetails.addAll(ipsDetails);
    }

}

public List<InvoiceProductsServicesDetail> getIpsDetails() {
    return ipsDetails;
    //return new InvoiceProductsServicesDetailDao().getIpsData() ;
}

public String getInvoiceNumber() {
    return invoiceNumber;
}

public void setInvoiceNumber(String invoiceNumber) {
    this.invoiceNumber = invoiceNumber;
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

public String getDeliveryNote() {
    return deliveryNote;
}

public void setDeliveryNote(String deliveryNote) {
    this.deliveryNote = deliveryNote;
}

public String getModeOfPayment() {
    return modeOfPayment;
}

public void setModeOfPayment(String modeOfPayment) {
    this.modeOfPayment = modeOfPayment;
}

public int getSupplierReference() {
    return supplierReference;
}

public void setSupplierReference(int supplierReference) {
    this.supplierReference = supplierReference;
}

public String getOtherReference() {
    return otherReference;
}

public void setOtherReference(String otherReference) {
    this.otherReference = otherReference;
}

public String getBuyerName() {
    return buyerName;
}

public void setBuyerName(String buyerName) {
    this.buyerName = buyerName;
}

public Date getDispatchDocumentDate() {
    return dispatchDocumentDate;
}

public void setDispatchDocumentDate(Date dispatchDocumentDate) {
    this.dispatchDocumentDate = dispatchDocumentDate;
}

public String getDispatchThrough() {
    return dispatchThrough;
}

public void setDispatchThrough(String dispatchThrough) {
    this.dispatchThrough = dispatchThrough;
}

public String getDestination() {
    return destination;
}

public void setDestination(String destination) {
    this.destination = destination;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getTermsOfdelivery() {
    return termsOfdelivery;
}

public void setTermsOfdelivery(String termsOfdelivery) {
    this.termsOfdelivery = termsOfdelivery;
}

public int getSerialNumber() {
    return serialNumber;
}

public void setSerialNumber(int serialNumber) {
    this.serialNumber = serialNumber;
}

public String getDescriptionOfGoodsOrService() {
    return descriptionOfGoodsOrService;
}

public void setDescriptionOfGoodsOrService(
        String descriptionOfGoodsOrService) {
    this.descriptionOfGoodsOrService = descriptionOfGoodsOrService;
}

public String getHsnCode() {
    return hsnCode;
}

public void setHsnCode(String hsnCode) {
    this.hsnCode = hsnCode;
}

public int getQuantity() {
    return quantity;
}

public void setQuantity(int quantity) {
    this.quantity = quantity;
}

public int getRate() {
    return rate;
}

public void setRate(int rate) {
    this.rate = rate;
}

public int getPercentDiscount() {
    return percentDiscount;
}

public void setPercentDiscount(int percentDiscount) {
    this.percentDiscount = percentDiscount;
}

public float getVat() {
    return vat;
}

public void setVat(float vat) {
    this.vat = vat;
}

public float getServiceTax() {
    return serviceTax;
}

public void setServiceTax(float serviceTax) {
    this.serviceTax = serviceTax;
}

public String getBuyerOrderNumber() {
    return buyerOrderNumber;
}

public void setBuyerOrderNumber(String buyerOrderNumber) {
    this.buyerOrderNumber = buyerOrderNumber;
}

public Invoice getInvoice() {
    return invoice;
}

public void setInvoice(Invoice invoice) {
    this.invoice = invoice;
}

public int getAmount() {
    return amount;
}

public void setAmount(int amount) {
    this.amount = amount;
}

public int getTotal() {
    return total;
}

public void setTotal(int total) {
    this.total = total;
}
}

我想将所有数据存储在mysql数据库表中。

2 个答案:

答案 0 :(得分:0)

您没有在发票bean中设置值。

通过单击“添加”添加新详细信息后,您将把值放入此新对象中。但是,然后在saveInvoice bean中保存一个新的Detail并尝试从bean中的属性中获取值。 (未设置,因此为NULL)这不起作用,因为属性未映射到表。

您还在代码中创建了超出必要的工作量并使其复杂化。如果您更改bean以从addRow方法获取新实例的详细信息或更新单元格编辑器中的属性,它应该可以工作。您可能还需要简化其他一些代码。

注意:我实际上建议你从头开始。从addRow开始,使用迭代方法解决问题,然后保存。调试是你的朋友。

更新xhtml输出以映射到属性。 E.g:

<f:facet name="input">
                    <h:inputText value="#{invoiceBean.($ipsDetail)}" size="3" />
                </f:facet>

另外:在你的页脚中,你引用了一个actionListener。将此更改为&#39;操作&#39;并删除该过程。让addRow方法将空行添加到详细信息列表中。映射这个细节。

<p:commandButton value="Add New Row" oncomplete="clickFirstRow()"
                action="#{invoiceBean.addRow}" update="invoiceTable"
                 />

此外,您的保存按钮可以通过单击要保存的所有行来处理应用程序并保存可编辑行。

<p:commandButton value="Save Invoice" ajax="false"
    onclick="tickAllRows()" action="#{invoiceBean.saveInvoiceData}" />

答案 1 :(得分:0)

尝试将bean的范围更改为“request”或“session”而不是“view scope”