0
votes

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>
2
Here <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 with process which are needed to be processed in addition to @this, when you click the said <p:commandButton>. The thing is that since ajax is set to true (it is actually default. Therefore, you can safely remove it for brevity) and process to @this, only the command button comes into play.Tiny
in your method BanCitizen() try to remove selected row first from your list datamodel and then do other stuff.Scorpion
@Tiny, i tried like you told but it doesn't work :((Haytham
@Scorpion i tried : public void BanCitizen(Citizen citizen) { citizens.remove(citizen); } But i doesnt work :((Haytham
Not citizens.remove(citizen); your <p:dataTable> tag use value of #{citizenCtr.datamodel} not citizens list.Scorpion

2 Answers

1
votes

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:

  1. Follow naming conventions. Method names begin with lowercase.

  2. 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.

  3. Check the logs to see if there are error messages.

0
votes

Why action on commandButton? Try actionListener...

<p:commandButton  actionListener="#{citizenCtr.banCitizen(citizen)}" value="Ban" ajax="true" update="mytable" process="@this">