我有一个简单的 crud 应用程序。它当前可以从列表中获取记录并将其显示在数据表中。我正在尝试使用模式对话框来更新选定的记录。但是,更新任何记录都不起作用。我已经尝试了几种可以在 stackoverflow 上找到的建议的可能解决方案,但没有任何效果。我刚开始学习 jsf 和 primefaces。我希望你能帮助我度过难关。我在这上面花了很多天。它已经令人沮丧,似乎在任何地方都找不到解决方案。谢谢 下面是代码
<!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;
}
}