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>
<p:commandButton action="#{citizenCtr.BanCitizen()}" value="Ban" ajax="true" update="mytable" process="@this">
,process="@this"
is the closest (or maybe the only) culprit. Either just remove it, if you want the form to be processed in its entirely or list the required components withprocess
which are needed to be processed in addition to@this
, when you click the said<p:commandButton>
. The thing is that sinceajax
is set totrue
(it is actually default. Therefore, you can safely remove it for brevity) andprocess
to@this
, only the command button comes into play. – TinyBanCitizen()
try to remove selected row first from your listdatamodel
and then do other stuff. – Scorpioncitizens.remove(citizen);
your<p:dataTable>
tag use value of#{citizenCtr.datamodel}
notcitizens
list. – Scorpion