I'm a beginner with JSF 2, PrimeFaces and EJB and I'm making a back-end application I successfully listed the database record on a datatable and I made a command button in the front of each record on the datatable, so I can delete a row when I click on that button , but i made all the code but when I click on the button nothing happened. Here is the code . and thank you.
The method in the EJB:
#Override
public void DeleteCitizen(Citizen citizen) {
Citizen detachCit = entityManager.merge(citizen);
entityManager.remove(detachCit);
//entityManager.remove(entityManager.merge(citizen));
}
The backing bean :
#ManagedBean
#SessionScoped
public class CitizenCtr {
private List<Citizen> citizens = new ArrayList<Citizen>();
private DataModel<Citizen> datamodel = new ListDataModel<Citizen>();
//the Model
Citizen cit = new Citizen();
//injection of the proxy
#EJB
CitizenServicesLocal citizenServiceLocal;
public List<Citizen> getCitizenss() {
citizens = citizenServiceLocal.ListAllCitizen();
return citizens;
}
public DataModel<Citizen> getDatamodel() {
datamodel.setWrappedData(citizenServiceLocal.ListAllCitizen());
return datamodel;
}
public void setDatamodel(DataModel<Citizen> datamodel) {
this.datamodel = datamodel;
}
public Citizen getCit() {
return cit;
}
public void setCit(Citizen cit) {
this.cit = cit;
}
// Login operation
public String TryLogin() {
String goTo = null;
Citizen citizenFound = citizenServiceLocal.Login(cit.getEmail(), cit.getPassword());
if (citizenFound != null) {
cit = citizenFound;
goTo = "/CitizenProfile/Profile?send-redirect=true";
System.out.println("Welcome you are logged In ");
} else {
System.out.println("please enter valid data ! ");
goTo = "/welcome?send-redirect=true";
}
return goTo;
}
//Subscribe operation
public String DoSubscribe() {
String Goto = null;
citizenServiceLocal.Subscribe(cit);
Goto = "/welcome?sendredirect=true";
return Goto;
}
//Update profile operation
public String DoUpdateProfile() {
String Goto = null;
citizenServiceLocal.updateProfile(cit);
Goto = "/CitizenProfile/Profile?sendredirect=true";
return Goto;
}
//Logout operation
public String DoLogout() {
String Goto = "/welcome?sendredirect=true";
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
return Goto;
}
public Citizen GetAllCitizen() {
List<Citizen> list;
list = citizenServiceLocal.ListAllCitizen();
return (Citizen) list;
}
// Ban Operation
public void BanCitizen() {
citizenServiceLocal.DeleteCitizen(cit);
}
}
And this is the XHTML file:
<h2>Citizen Management</h2>
<p:link action="#{citizenCtr.BanCitizen()}" value="erase" >
<p:ajax update="mytable"></p:ajax>
</p:link>
<p:dataTable id="mytable"
value="#{citizenCtr.datamodel}"
var="citizen">
<f:facet name="header"> List of Citizens </f:facet>
<p:column>
<f:facet name="header">
<h:outputText value="CIN"></h:outputText>
</f:facet>
<h:outputText value="#{citizen.CIN}"></h:outputText>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Name"></h:outputText>
</f:facet>
<h:outputText value="#{citizen.firstName}"></h:outputText>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="LastName"></h:outputText>
</f:facet>
<h:outputText value="#{citizen.scondName}"></h:outputText>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Addess"></h:outputText>
</f:facet>
<h:outputText value="#{citizen.email}"></h:outputText>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Email"></h:outputText>
</f:facet>
<h:outputText value="#{citizen.password}"></h:outputText>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Password"></h:outputText>
</f:facet>
<h:outputText value="#{citizen.adresse}"></h:outputText>
</p:column>
<p:column>
<p:commandButton action="#{citizenCtr.BanCitizen()}"
value="Ban"
ajax="true"
update="mytable"
process="#this">
</p:commandButton>
</p:column>
</p:dataTable>
It is hard to follow your logic, but you are calling DeleteCitizen(Citizen) using a this.cit value which is not clear where it has been set.
If you are using EL 2.2 (Servlet 3.0), you could do this:
In JSF:
<p:commandButton action="#{citizenCtr.banCitizen(citizen)}" value="Ban" ajax="true" update="mytable" process="#this">
In controller
public void banCitizen(Citizen citizenToDelete) {
citizenServiceLocal.deleteCitizen(citizenToDelete);
}
Notes to improve:
Follow naming conventions. Method names begin with lowercase.
Getters may be called several times for each request. Having to query the DB several times for all the citizens for each request will be time consuming. For example, store the citizen's list as a property of the controller; the getter should just return that value. When an action that modifies the contents of the list is performed (v.g. banCitizen(Citizen)), then that action should take care of updating the values of the attribute.
Check the logs to see if there are error messages.
Why action on commandButton? Try actionListener...
<p:commandButton actionListener="#{citizenCtr.banCitizen(citizen)}" value="Ban" ajax="true" update="mytable" process="#this">
Related
I have a datatable where one of the columns is editable.
As long as i dont use my filter i can navigate through my list and can edit the entry in any row i want. I dont get any errors and my changes are persisted in the DB correctly.
But after i once used any of the filter everything goes sideways. The filter itself works properly.
But when i now try to edit any entry the event.oldValue() from my CellEditEvent is always null and i cant persist my entries.
And only the first page of my paginator is filled with data. Every other page is empty.
Here is the view:
<div class="Container100">
<p:dataTable paginator="true" rows="15" var="listReportingParticipantOverview" value="#{reportingBean.listReportingParticipantOverview}"
id="participantOverviewId" widgetVar="participantOverviewId"
editable="true" editMode="cell" editInitEvent="dblclick"
emptyMessage="#{msg['system.no_result']}"
filteredValue="#{reportingBean.filteredListReportingParticipantOverview}">
<p:ajax event="cellEdit" listener="#{reportingBean.onCellEdit}" update=":reportingForm:msgs"/>
<p:column headerText="#{msg['model.reporting.participant_overview.name']}" filterBy="#{listReportingParticipantOverview.name}" filterMatchMode="contains">
<h:outputText value="#{listReportingParticipantOverview.name}" />
</p:column>
<p:column headerText="#{msg['model.reporting.participant_overview.dateOfBirth']}" filterBy="#{listReportingParticipantOverview.dateOfBirth}" filterMatchMode="contains">
<h:outputText value="#{listReportingParticipantOverview.dateOfBirth}" />
</p:column>
<p:column headerText="#{msg['model.reporting.participant_overview.email']}" filterBy="#{listReportingParticipantOverview.email}" filterMatchMode="contains">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{listReportingParticipantOverview.email}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{listReportingParticipantOverview.email}" style="width:100%" label="name"/>
</f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
</div>
Here is the bean:
#SessionScoped
#ManagedBean(name = "reportingBean2")
public class ReportingBean2 extends BeanController implements Serializable {
List<ReportingParticipantOverview> listReportingParticipantOverview = new ArrayList<ReportingParticipantOverview>();
List<ReportingParticipantOverview> filteredListReportingParticipantOverview;
ContactRepository contactRepository;
ContactRepository contactRepositoryOtherDBContext;
CommunicationRepository communicationRepositoryOtherDBContext;
private AbstractApplicationContext otherDBContext;
public void initData() {
this.listReportingParticipantOverview = this.contactRepositoryOtherDBContext
.selectParticipantOverviewReporting();
}
#PostConstruct
public void init() {
otherDBContext = new ClassPathXmlApplicationContext("applicationContext_otherDB.xml");
this.contactRepository = this.getApplicationContext().getBean(ContactRepository.class);
this.contactRepositoryOtherDBContext = otherDBContext.getBean(ContactRepository.class);
this.communicationRepositoryOtherDBContext = otherDBContext.getBean(CommunicationRepository.class);
}
public void onCellEdit(CellEditEvent event) {
Object oldValue = event.getOldValue();
Object newValue = event.getNewValue();
// ...
}
public List<ReportingParticipantOverview> getListReportingParticipantOverview() {
return listReportingParticipantOverview;
}
public void setListReportingParticipantOverview(
List<ReportingParticipantOverview> listReportingParticipantOverview) {
this.listReportingParticipantOverview = listReportingParticipantOverview;
}
public List<ReportingParticipantOverview> getFilteredListReportingParticipantOverview() {
return filteredListReportingParticipantOverview;
}
public void setFilteredListReportingParticipantOverview(
List<ReportingParticipantOverview> filteredListReportingParticipantOverview) {
this.filteredListReportingParticipantOverview = filteredListReportingParticipantOverview;
}
}
}
I am using Java 8, Eclipse 2021-03 and Primefaces 10.0.0.
Any help appreciated :-)
I am trying to perform a row edit in primefaces databable. My problem is when I key in new value in inputText in CellEditor
and click edit (primefaces icon) onCellEdit method is calledsuccessfully but does not pick the newly value in the inputText it pick value that was initially in the datatable. What Am I doing wrong? I am using primefaces 3.5
Here in jsf code
<h:form id="form1">
<p:growl id="messages" showDetail="true"/>
<p:panel header="Registered Devices" style="min-height: 400px;" id="paneldevices">
<p:dataTable emptyMessage="No Device Registered" editable="true" widgetVar="deviceTable" id="idGrid" value="#{deviceMgdBean.devices}" var="item" >
<p:ajax event="rowEdit" listener="#{deviceMgdBean.onCellEdit()}" update=":form1:messages"/>
<p:ajax event="rowEditCancel" listener="#{deviceMgdBean.onCancel}" update=":form1:messages" />
<p:column headerText="Options" style="width:50px">
<p:rowEditor />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Device_name"/>
</f:facet>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{item.device_name}"/>
</f:facet>
<f:facet name="input">
<p:inputText value="#{item.device_name}" style="width:80%" />
</f:facet>
</p:cellEditor>
</p:column>
</h:form>
Here is my managed Bean
#ManagedBean
#ViewScoped
public class DeviceMgdBean implements Serializable {
public List<Devices> getDevices()
{
List<Devices> l=getDevdao().getDevices();//devices fetched from database
return l;
}
public void onCellEdit(RowEditEvent event)
{
Devices devo=(Devices) event.getObject();
FacesMessage msg = new FacesMessage("Device Edit","Test:"+devo.getDeviceName());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onCancel(RowEditEvent event)
{
FacesMessage msg = new FacesMessage("Item Cancelled");
FacesContext.getCurrentInstance().addMessage(null, msg);
Devices devo=(Devices) event.getObject();
}
}
class Devices {
private int device_id;
private String device_name;
//getter and setters
}
private List<Devices> l;
public List<Devices> getDevices() {
if(l==null) l=getDevdao().getDevices();//devices fetched from database
return l;
}
I have a readonly ace:datatable composed of 4 columns
I wish to make the fourth column "Process Limit" editable in this datatable
Can i do this ?
This is the xhtml code :
<ace:dataTable value="#{bankProcessLimitManagement.bankProcessLimitBean}"
var="name" style="width: 50% !important;" id="namesTable"
rowSelectListener="#{bankProcessLimitManagement.rowSelectListener}"
rowUnselectListener="#{bankProcessLimitManagement.rowDeSelectListener}"
selectionMode="single" paginator="true" rows="10">
<ace:column headerText="LatinName">
<h:outputText value="#{name.latinName}"></h:outputText>
</ace:column>
<ace:column headerText="Arabic Name">
<h:outputText value="#{name.arabicName}"></h:outputText>
</ace:column>
<ace:column headerText="Process Type">
<h:outputText value="#{name.processType}"></h:outputText>
</ace:column>
<ace:column headerText="Process Limit">
<h:outputText value="#{name.limit}"></h:outputText>
</ace:column>
</ace:dataTable>
This is the correspondent bean:
#ManagedBean(name="bankProcessLimitManagement")
#ViewScoped
public class BankProcessLimitManagement {
// Render for the datatable
private boolean renderTable = false;
// List linked to the datatable
private List<BankProcessLimitBean> bankProcessLimitBean;
// Selected Row
private BankProcessLimitBean selectedBankProcessLimit;
public void rowSelectListener(SelectEvent event) {
selectedBankProcessLimit = (BankProcessLimitBean) event.getObject();
}
public void rowDeSelectListener(UnselectEvent event) {
selectedBankProcessLimit = null;
}
// Getters
public List<BankProcessLimitBean> getBankProcessLimitBean() { return bankProcessLimitBean; }
public boolean isRenderTable() { return renderTable; }
public BankProcessLimitBean getSelectedBankProcessLimit() { return selectedBankProcessLimit; }
// Setters
public void setRenderTable(boolean renderTable) { this.renderTable = renderTable; }
public void setBankProcessLimitBean(List<BankProcessLimitBean> bankProcessLimitBean) { this.bankProcessLimitBean = bankProcessLimitBean; }
public void setSelectedBankProcessLimit(BankProcessLimitBean selectedBankProcessLimit) { this.selectedBankProcessLimit = selectedBankProcessLimit; }
}
Thanks in advance
As said in the documentation you should do:
<ace:column headerText="Process Limit">
<ace:cellEditor>
<f:facet name="output">
<h:outputText value="#{name.limit}"/>
</f:facet>
<f:facet name="input">
<h:inputText value="#{name.limit}"/>
</f:facet>
</ace:cellEditor>
</ace:column>
You need of course to add a form wrapping your table, and to include a button or a link in order to submit the data.
I'm working with JSF and primefaces. I want to update a row when I click on the button (pencil). I used the first code from this tutorial (Primefaces docs), but dosent work in my case.
the problem is : when i click the button (pencil) to update a row, nothing change. i have always the old values
This is my JSF code :
<f:view>
<h:form id="form">
<p:growl id="msgs" showDetail="true"/>
<p:dataTable id="tservice" var="item" value="#{serviceMBean.services}" editable="true" style="margin-bottom:20px">
<f:facet name="header"> edit </f:facet>
<p:ajax event="rowEdit" listener="#{serviceMBean.onRowEdit}" update=":form:msgs" />
<p:ajax event="rowEditCancel" listener="#{serviceMBean.onRowCancel}" update=":form:msgs" />
<p:column headerText="Libelle du service">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{item.libelleservice}" /></f:facet>
<f:facet name="input">
<p:inputText value="#{item.libelleservice}" style="width:100%" label="Libelle"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Utilisateurs/Service">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{item.nbruserservice}" /></f:facet>
<f:facet name="input">
<p:inputText value="#{item.nbruserservice}" style="width:100%" label="Nbre Users"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column style="width:32px">
<p:rowEditor />
</p:column>
And this is my managed bean code:
#Named(value = "serviceMBean")
#ViewScoped
public class ServiceMBean implements Serializable {
private List<Service> serviceList;
private Service s;
#EJB
private ServiceManager serviceManager;
public ServiceMBean() {}
public void modifierService(Service s1) {
this.serviceManager.updateService(s1);
}
public void onRowEdit(RowEditEvent event) {
Service s2 = (Service) event.getObject();
System.out.println("--->" + s2.getNbruserservice());
FacesMessage msg = new FacesMessage("Service Modifié", ((Service) event.getObject()).getLibelleservice());
this.modifierService(s2);
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onRowCancel(RowEditEvent event) {
FacesMessage msg = new FacesMessage("Modification annulée", ((Service) event.getObject()).getLibelleservice());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
And this is my code for ServiceManager:
public void updateService(Object service) {
em.merge(service);
}
I think the setter in my JSF page doesn't work. Any solutions or suggestions?
Solution : Add a void method with #PostConstruct.
Example : #PostConstruct
#PostConstruct
public void initialiser() {
serviceList = this.getServices();
}
Don't forget the getter and setter of serviceList
In the JSF page, the value of datatable will be : value="#{ServiceMBean.serviceList}"
I'm having some trouble when trying to select single rows in a datatable (I can't figure out what's going on, because my code is based on the primefaces showcase).
The table is filled first with result matches, then I'm trying to select a row, and finally trying to do some process. The problem appears when I try to process that single row, none of the actual methods its being called.
<h:form id="form2">
<p:dialog header="Agregar Producto" widgetVar="dlgAgregarProducto" modal="true" width="800" height="300">
<h:outputText value="Código"/>
<h:inputText value="#{promocionController.strIdProducto}"/>
<h:outputText value="Nombre"/>
<h:inputText value="#{promocionController.strNombre}"/>
<p:commandButton id="btnBuscarProducto" value="Buscar" action="#{promocionController.buscarProductosIdNombre}" update="tableBusqueda"/>
<p:commandButton id="btnAgregarProducto" value="Agregar" action="#{promocionController.agregarProductos}" />
<p:dataTable id="tableBusqueda" var="prod" value="#{promocionController.obtenerProductos()}"
rowKey="#{prod.idProducto}" selection="#{promocionController.arrProductosSeleccionados}" selectionMode="single">
<p:column style="width:24%">
<f:facet name="header">
<h:outputText value="Código"/>
</f:facet>
<h:outputText value="#{prod.idProducto}"/>
</p:column>
<p:column style="width:24%">
<f:facet name="header">
<h:outputText value="Foto"/>
</f:facet>
<h:outputText value="foto"/>
</p:column>
<p:column style="width:24%">
<f:facet name="header">
<h:outputText value="Descripción"/>
</f:facet>
<h:outputText value="#{prod.descripcion}"/>
</p:column>
<p:column style="width:24%">
<f:facet name="header">
<h:outputText value="Marca"/>
</f:facet>
<h:outputText value=""/>
</p:column>
<p:column style="width:24%">
<f:facet name="header">
<h:outputText value="Modelo"/>
</f:facet>
<h:outputText value="#{prod.modelo}"/>
</p:column>
<p:column style="width:24%">
<f:facet name="header">
<h:outputText value="Precio"/>
</f:facet>
<h:outputText value="#{prod.precio}"/>
</p:column>
</p:dataTable>
</p:dialog>
</h:form>
Backing bean:
#ManagedBean(name = "promocionController")
#SessionScoped
public class PromocionCO {
arrProductosPromocion = new ArrayList<Producto>();
arrProductosSeleccionados = new ArrayList<Producto>();
}
public void buscarProductosIdNombre() {
productoDAO = new ProductoDAO();
arrProductosBusqueda = new ArrayList<Producto>();
arrProductosSeleccionados = new ArrayList<Producto>();
// si la búsqueda es por nombre
if (strIdProducto.compareTo("") == 0) {
arrProductosBusqueda = productoDAO.obtenerProductosPorNombre(strNombre);
} // si la búsqueda es por id
else {
producto = productoDAO.obtenerProductoPorId(strIdProducto);
if (producto != null) {
arrProductosBusqueda.add(producto);
} else {
arrProductosBusqueda = null;
}
}
}
public void agregarProductos() {
for (int i = 0; i < arrProductosSeleccionados.size(); i++) {
arrProductosPromocion.add(arrProductosSeleccionados.get(i));
}
}
public ArrayList<Producto> obtenerProductos() {
return arrProductosBusqueda;
}
public ArrayList<Producto> getArrProductosSeleccionados() {
return arrProductosSeleccionados;
}
public void setArrProductosSeleccionados(ArrayList<Producto> arrProductosSeleccionados) {
if (arrProductosSeleccionados != null) {
this.arrProductosSeleccionados = arrProductosSeleccionados;
}
}
This is because your selection attribute points to an ArrayList of Producto instead of a single element. Since you are using datatable with selectionMode="single" the selected attribute should point a single element in your backing bean:
Producto selectedProducto;
public Producto getSelectedProducto() {
return selectedProducto;
}
public void setSelectedProducto(Producto producto) {
selectedProducto = producto;
}
Finally, specify this element in your dataTable selection:
<p:dataTable id="tableBusqueda" var="prod" selectionMode="single"
value="#{promocionController.obtenerProductos}"
selection="#{promocionController.selectedProducto}">
Now after selecting a row the public void setSelectedProducto(Producto producto) method will be called.
Unrelated to the concrete problem, it is more preferable to use getter/setter methods when pointing to a value in your backing bean:
<p:dataTable id="tableBusqueda" var="prod"
value="#{promocionController.obtenerProductos}" .../>
In general, an appropriate getter/setter should looks like get<NameOfTheObject>, in your case:
public List<Producto> getObtenerProductos()