Reputation: 854
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>
Upvotes: 0
Views: 1690
Reputation: 11
Why action on commandButton? Try actionListener...
<p:commandButton actionListener="#{citizenCtr.banCitizen(citizen)}" value="Ban" ajax="true" update="mytable" process="@this">
Upvotes: 0
Reputation: 24885
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.
Upvotes: 1