简体   繁体   中英

record not updating with primefaces 6.2

I have a simple crud application. it can currently fetch record from a list and display it in a datatable. I am trying to use a modal dialog to update selected record. However updating any of the records don't work. I have tried several suggested possible solutions I could find on stackoverflow, nothing is working. I am just getting to learn jsf and primefaces. I am hoping you can help me through it. I have spend many days on this. it's already frustrating and don't seem to find the solution anywhere. Thank you Below is the code

<!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:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">

<h:head>
<meta charset="utf-8" name="viewport"
    content="width=device-width, initial-scale=1"
    http-equiv="X-UA-Conpatible" />
<h:outputStylesheet library="css" name="bootstrap.min.css" />
<title>JSF CRUD Assignment</title>
<style type="text/css">
.btnWidth {
width: 80px;
}
</style>
</h:head>
<h:body>
<h:form id="createCustomerRecord" class="form-horizontal">
    <div class="form-group">
        <div class="col-sm-4"></div>
        <div class="col-sm-4">
            <h2>Create Customer Record</h2>
        </div>
    </div>
    <hr />
    <div class="form-group">
        <h:outputLabel for="firstName" class="control-label col-sm-4">First Name:</h:outputLabel>
        <div class="col-sm-4">
            <h:inputText value="#{customerBean.newCustomer.firstName}"
                class="form-control">

            </h:inputText>
        </div>
    </div>
    <div class="form-group">
        <h:outputLabel for="lastName" class="control-label col-sm-4">Last Name:</h:outputLabel>
        <div class="col-sm-4">
            <h:inputText value="#{customerBean.newCustomer.lastName}"
                class="form-control">

            </h:inputText>
        </div>
    </div>

    <div class="form-group">
        <div class="col-sm-4"></div>
        <div class="col-sm-4">
            <div class="col-sm-2">
                <h:commandButton value="Save"
                    action="#{customerBean.saveCustomerDetails}"
                    class="btn btn-success btnWidth" />
            </div>

        </div>
    </div>
</h:form>
<center>
    <h2>
        <h:outputText value="Customer Records" />
    </h2>
</center>
<h:form id="customerForm">
    <p:growl id="msgs" showDetail="true" skipDetailIfEqualsSummary="true" />
        <p:dataTable id="basicDT" var="customer"
            reflow="true"
            value="#{customerBean.customerList}"
            selection="#{customerBean.selectedCustomer}"
            rowKey="#{customer.customerId}" paginator="true" rows="10"
            rowSelectMode="add" paginatorPosition="bottom">
            <p:column headerText="Id">
                <h:outputText value="#{customer.customerId}" />
            </p:column>

            <p:column headerText="First Name">
                <h:outputText value="#{customer.firstName}" />
            </p:column>

            <p:column headerText="Last Name">
                <h:outputText value="#{customer.lastName}" />
            </p:column>

            <p:column style="width:3rem;text-align: center" exportable="false">
                <p:commandButton update=":customerForm:customerDetail"
                    value="Update" oncomplete="PF('customerDialog').show()"
                    process="@this">
                    <f:setPropertyActionListener value="#{customer}"
                        target="#{customerBean.selectedCustomer}" />
                    <p:resetInput target="customerForm:customerDetail" />
                </p:commandButton>
            </p:column>
        </p:dataTable>
    <p:dialog header="Customer Info" widgetVar="customerDialog"
        modal="true" showEffect="fade" hideEffect="fade" resizable="false">
        <p:outputPanel id="customerDetail" style="text-align:center;">
            <p:outputPanel rendered="#{not empty customerBean.selectedCustomer}">

                <div class="form-group">
                    <div class="col-sm-4"></div>
                    <div class="col-sm-4">
                        <h2>Edit Customer Record</h2>
                    </div>
                </div>
                <hr />
                <div class="form-group">
                    <h:outputLabel for="customerId" class="control-label col-sm-4">Customer Id:</h:outputLabel>
                    <div class="col-sm-4">
                        <h:inputText id="customerId"
                            value="#{customerBean.selectedCustomer.customerId}"
                            disabled="true"
                            class="form-control">

                        </h:inputText>
                    </div>
                </div>
                <div class="form-group">
                    <h:outputLabel for="firstName" class="control-label col-sm-4">First Name:</h:outputLabel>
                    <div class="col-sm-4">
                        <h:inputText id="firstName"
                            value="#{customerBean.selectedCustomer.firstName}"
                            class="form-control">

                        </h:inputText>
                    </div>
                </div>
                <div class="form-group">
                    <h:outputLabel for="lastName" class="control-label col-sm-4">Last Name:</h:outputLabel>
                    <div class="col-sm-4">
                        <h:inputText id="lastName"
                            value="#{customerBean.selectedCustomer.lastName}"
                            class="form-control">
                        </h:inputText>
                    </div>

                </div>
            </p:outputPanel>
        </p:outputPanel>
        <f:facet name="footer">
            <p:commandButton value="Save"
                ajax="false"
                action="#{customerBean.updateCustomerDetails}"
                update=":form:customerDetail" process="form:customerDetail @this" 
                />
        </f:facet>
    </p:dialog>

    </h:form>
   </h:body>
   </html>

import java.util.List;
import java.util.UUID;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.michi.bess.customerservice.models.Customer;
import org.michi.bess.customerservice.service.DatabaseOperation;
import org.primefaces.PrimeFaces;

@ManagedBean
@RequestScoped
public class CustomerBean {

public List<Customer> customerList;

private Customer newCustomer = new Customer();
private Customer selectedCustomer = new Customer();

@PostConstruct
public void init() {
    customerList = DatabaseOperation.getCustomerList();
}

public List<Customer> customerList() {
    return customerList;
}

public String editCustomerRecord() {
    return "record edited"; //

}

public String editCustomerRecord(int studentId) {
    return "record edited"; //

}

public void updateCustomerDetails() {
    System.out.println("hitting update in customer bean");
    
    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Customer Updated"));
    DatabaseOperation.editCustomerRecord(selectedCustomer);
    PrimeFaces.current().executeScript("PF('customerDialog').hide()");
    PrimeFaces.current().ajax().update("customerForm:messages", "form:basicDT");
    
}

public String deleteCustomerRecord(int studentId) {
    //return DatabaseOperation.deleteCustomerRecord(studentId);
    return null;
}

public void saveCustomerDetails() {
    System.out.println("hitting customer bean save method");

    System.out.println("new customer");
    this.newCustomer.setCustomerId(UUID.randomUUID().toString().replaceAll("-", "").substring(0, 9));
    DatabaseOperation.saveCustomerDetails(newCustomer);
    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Customer Added"));

    PrimeFaces.current().executeScript("PF('customerDialog').hide()");
    PrimeFaces.current().ajax().update("customerForm:messages", "form:basicDT");
}

public void setCustomerList(List<Customer> customerList) {
    this.customerList = customerList;
}

public Customer getSelectedCustomer() {
    return selectedCustomer;
}

public void setSelectedCustomer(Customer selectedCustomer) {
    this.selectedCustomer = selectedCustomer;
}

public Customer getNewCustomer() {
    return newCustomer;
}

public void setNewCustomer(Customer newCustomer) {
    this.newCustomer = newCustomer;
}

public List<Customer> getCustomerList() {
    return customerList;
}

}

import java.util.Objects;

public class Customer {

private String customerId;
private String firstName;
private String lastName;
public String getCustomerId() {
    return customerId;
}
public void setCustomerId(String customerId) {
    this.customerId = customerId;
}
public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}
@Override
public int hashCode() {
    return Objects.hash(customerId);
}
@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (!(obj instanceof Customer))
        return false;
    Customer other = (Customer) obj;
    return customerId == other.customerId;
}


}

I'm also doing crud datatable, i've run your jsf crud application and update work, maybe the problem is in: DatabaseOperation.editCustomerRecord(selectedCustomer);

reference crud

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM