我尝试使用Click进行单元格编辑来创建DataTables,如下所示:https://www.primefaces.org/showcase/ui/data/datatable/edit.xhtml
我遇到的问题是表格不会考虑修改。 从MySQL数据库显示数据没有问题。表格正确加载,没有错误。当我尝试修改单元格时,它会按预期变为输入。但是当我更改值并按回车键时,单元格将返回其原始值。如果我在该单元格上重新进入编辑模式,则会再次出现我的修改。未在数据库中进行修改。
我对JSF和PrimeFaces不太放心,我肯定错过了一些明显的东西。
这是我的代码:
Managed Bean:
package Application;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import org.primefaces.event.CellEditEvent;
import org.primefaces.event.RowEditEvent;
@Named(value = "plantCtrl")
@ViewScoped
public class PlantCtrl implements Serializable {
@EJB
private PlantDAO plantDAO;
public PlantCtrl() {
}
@PostConstruct
public void init(){
}
public List<Plant> getPlants() {
return plantDAO.allPlants();
}
public PlantDAO getPlantDAO() {
return plantDAO;
}
public void setPlantDAO(PlantDAO plantDAO) {
this.plantDAO = plantDAO;
}
public void onRowEdit(RowEditEvent event) {
FacesMessage msg = new FacesMessage("Plant Edited");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onRowCancel(RowEditEvent event) {
FacesMessage msg = new FacesMessage("Edit Cancelled");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onCellEdit(CellEditEvent event) {
Object oldValue = event.getOldValue();
Object newValue = event.getNewValue();
if(newValue != null && !newValue.equals(oldValue)) {
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Cell Changed", "Old: " + oldValue + ", New:" + newValue);
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
}
PlantDAO:
package Application;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@Stateless
public class PlantDAO {
@PersistenceContext(unitName = "CarnivorousGardenPU")
private EntityManager em;
public List<Plant> allPlants() {
Query query = em.createNamedQuery("Plant.findAll");
return query.getResultList();
}
public void add(Plant p) {
em.persist(p);
em.flush();
}
public void edit(Plant p) {
em.merge(p);
em.flush();
}
public void remove(Plant p) {
em.remove(em.merge(p));
em.flush();
}
}
XHTML:
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title>Dashboard</title>
</h:head>
<h:body>
<ui:include src="inc/header.xhtml" />
<h:form id="plantAdminList">
<p:growl id="msgs" showDetail="true"/>
<p:dataTable id="plantsTable" var="plant" value="#{plantCtrl.plants}" editable="true" editMode="cell" widgetVar="cellPlants">
<f:facet name="header">
Cell Editing with Click and RightClick
</f:facet>
<p:ajax event="cellEdit" listener="#{plantCtrl.onCellEdit}" update=":plantAdminList:msgs, :plantAdminList:plantsTable" />
<p:column headerText="Id">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{plant.genusPlant}" /></f:facet>
<f:facet name="input"><p:inputText id="modelInput" value="#{plant.genusPlant}" style="width:96%"/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Price">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{plant.pricePlant}" /></f:facet>
<f:facet name="input"><p:inputText value="#{plant.pricePlant}" style="width:96%" label="Price"/></f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
</h:form>
<ui:include src="/inc/footer.xhtml" />
</h:body>
</html>
Plant.java由Netbeans自动生成为数据库中的实体类。我没有因为没有超载此帖而发帖,但如果需要,请不要犹豫。
答案 0 :(得分:1)
像这样修改托管bean的几行
api.adwords.developerToken=123axxxxxxxxxxxxxxxxxx
api.adwords.clientId=xxxxxxxxxx.apps.googleusercontent.com
api.adwords.clientSecret=zZxxxxxTxxxxxxxxxxx
api.adwords.clientCustomerId=123-456-7890
api.adwords.refreshToken=1/dyOIp7ki-xxxxxxxxxxxxxxxxxxxxxxxx
它将按您的意愿工作:当您更改值并按Enter键时,单元格将反映您的更改。
请注意:
如果要保留对数据库的更改,则需要稍微修改packagingOptions {
exclude 'META-INF/ECLIPSE_.SF'
exclude 'META-INF/ECLIPSE_.RSA'
}
compile("one-of-your-dependency:1.0.0"){
exclude group: 'org.eclipse.paho'
}
并在托管bean中创建其他逻辑以检测修改并将其传递给现有的//....
List<Plant> plants;
@PostConstruct
public void init(){
plants=plantDAO.allPlants();
}
public List<Plant> getPlants() {
return plants;
}
//....
方法。编码最小值为:
像这样修改数据表
p:dataTable
修改托管bean内的plantDao.edit
p:dataTable id="plantsTable" var="plant" value="#{plantCtrl.plants}" editable="true" widgetVar="cellPlants">
<p:ajax event="rowEdit" listener="#{plantCtrl.onRowEdit}"
update=":plantAdminList:msgs, :plantAdminList:plantsTable" />
....
<p:column style="width:32px">
<p:rowEditor />
</p:column>
</p:dataTable>