primefaces单元内编辑不会更新数据库中的数据 [英] primefaces in-cell-editing not update data in database
问题描述
我在JEE项目中使用Primeface 4.0,JSF 2和jpa,我在单元格编辑中使用了Primefaces展示数据表中显示的示例。这是HTML代码
I use Primeface 4.0 , JSF 2 and jpa in a JEE project , i used the example shown in Primefaces showcase data table in cell editing. Here is the HTML code
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:rich="http://richfaces.org/rich"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:p="http://primefaces.org/ui"
xmlns:c="http://java.sun.com/jsp/jstl/core"
template="/WEB-INF/templates/default.xhtml">
<ui:define name="content">
<center>
<rich:panel style="width : 800px; height : 551px; " >
<f:facet name="header" >
<h:outputText value="Tableau des articles" align="center" style="FONT-SIZE: small;"/>
</f:facet>
<h:form id="form">
<p:growl id="messages" showDetail="true"/>
<p:contextMenu for="cars" widgetVar="cMenu">
<p:menuitem value="Edit Cell" icon="ui-icon-search" onclick="PF('carsTable').showCellEditor();return false;"/>
<p:menuitem value="Hide Menu" icon="ui-icon-close" onclick="PF('cMenu').hide()"/>
</p:contextMenu>
<p:dataTable id="cars" var="car" value="#{articlesbean.LMatpilotaccess1}" editable="true" editMode="cell" widgetVar="carsTable">
<f:facet name="header">
Matériel du pilotage et accessoires
</f:facet>
<p:ajax event="cellEdit" listenner="#{articlesbean.onCellEdit()}" update=":form:messages" />
<p:column headerText="Serie" style="width:25%">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{car.serie}" /></f:facet>
<f:facet name="input"><p:inputText id="modelInput" value="#{car.serie}" style="width:96%"/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Prix unitaire HTVA" style="width:25%">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{car.puhtva}" /></f:facet>
<f:facet name="input"><p:inputText id="modelInput5" value="#{car.puhtva}" style="width:96%"/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Montant HTVA" style="width:25%">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{car.monthtva}" /></f:facet>
<f:facet name="input"><p:inputText id="modelInput8" value="#{car.monthtva}" style="width:96%"/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Montant TTC" style="width:25%">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{car.montttc}" /></f:facet>
<f:facet name="input"><p:inputText id="modelInput9" value="#{car.montttc}" style="width:96%"/></f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
</h:form>
</rich:panel>
</center>
</ui:define>
</ui:composition>
这就是Bean
package com.pfe.controller;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import org.primefaces.component.datatable.DataTable;
import org.primefaces.event.CellEditEvent;
import com.pfe.data.ArticlesDAO;
import com.pfe.model.Matpilotaccess1;
import com.pfe.model.Matpilotaccess2;
import com.pfe.model.Poteaux;
import com.pfe.model.Travgc1;
import com.pfe.model.Travgc2;
import com.pfe.model.Travresurbain;
@ManagedBean(name="articlesbean")
@ViewScoped
public class ArticlesBean implements Serializable{
@Inject
private ArticlesDAO articleDAO;
@Inject
private Matpilotaccess1 matpilotaccess1;
@Inject
private Matpilotaccess2 matpilotaccess2;
@Inject
private Poteaux poteaux ;
@Inject
private Travgc1 travgc1;
@Inject
private Travgc2 travgc2;
@Inject
private Travresurbain travresurbain;
private List LMatpilotaccess1 = new ArrayList();
private List LMatpilotaccess2 = new ArrayList();
private List LPoteaux = new ArrayList();
private List LTravgc1 = new ArrayList();
private List LTravgc2 = new ArrayList();
private List LTravresurbain = new ArrayList();
public void onCellEdit(CellEditEvent event) {
Object oldValue = event.getOldValue();
Object newValue = event.getNewValue();
System.out.println("////////////////////////);
if(newValue != null && !newValue.equals(oldValue)) {
DataTable s = (DataTable) event.getSource();
Matpilotaccess1 d = (Matpilotaccess1) s.getRowData();
articleDAO.Updatetable(d);
System.out.println("///////////////////");
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Cell Changed", "Old: " + oldValue + ", New:" + newValue);
FacesContext.getCurrentInstance().addMessage(null, msg);
}
} ///// getters and setters
这里的问题是,即使/ //////////////不会显示,这意味着HTML代码中存在问题,导致无法执行onCellEdit函数!!!
the problem here is that even /////////////// are not displayed which means there is a problem in the HTML code that prevent the execution of onCellEdit function !!!
推荐答案
我认为您只有一个问题:您的代码未调用articlebean.onCellEdit()方法。
I think you have only one problem: your code not call your articlesbean.onCellEdit() method.
这是我的简单示例,工作正常(但我没有将richfaces组件与primefaces混合使用):
Here is my simple which works fine (but i not mixed richfaces components with primefaces):
<h:form id="testForm">
<p:growl id="messages" showDetail="true"/>
<p:outputPanel id="testContainer">
<p:dataTable id="testTable" value="#{tableBean.data}" var="entry" editable="true" editMode="cell">
<p:ajax event="cellEdit" listener="#{tableBean.onCellEdit}" process="@this" update=":testForm:messages"/>
(...)
用户在表中编辑后,该消息会正确显示。请尝试将Richfaces面板替换为 primefaces面板,这可能是发生此问题的原因。
The message correctly appear after the user edit in table. Please try to replace richfaces panel to primefaces panel may be that is occured this problem.
如果p:ajax中存在一些更新问题,则可以使用远程命令。但是在这种情况下,您需要重新设计Articlesbean中的方法,以便:
If you have some update problem in p:ajax you can use RemoteCommand. But in this case you need redesign your method in articlesbean to:
public void onCellEdit() {
//do some thing
和xhtml:
<p:remoteCommand name="onCellEdit" action="#{articlesbean.onCellEdit()}" update=":form:messages" />
<p:dataTable id="cars" var="car" value="#{articlesbean.LMatpilotaccess1}" editable="true" editMode="cell" widgetVar="carsTable">
(...)
<p:ajax event="cellEdit" oncomplete="onCellEdit()" />
RemoteCommand提供了一种直接从javascript执行JSF支持bean方法的方法。
RemoteCommand provides a way to execute JSF backing bean methods directly from javascript.
请尝试!
这篇关于primefaces单元内编辑不会更新数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!