0
votes

I am using PrimeFaces 6.0 components:

<p:commandButton type="submit" value="Create Customer"
    icon="ui-icon-check"
    actionListener="#{newCustomerBean.saveNewCustomer}"
    update = "@form"
    oncomplete="ajaxUploadFile();"/>

<p:inputText id="saveCustomerId" value ="#{newCustomerBean.savedKundeId}"/>

and I want to execute the following sequence of actions with them:

1.) Execute the actionListener method on the backing bean to save a customer;

2.) Update the form field saveCustomerId with the id of the customer that is saved on step (1). The actionListener method generates a customer Id after the successful save and stores is as a bean property;

3.) Execute the Java Script method ajaxUploadFile()

According to the link

Execution order of events when pressing PrimeFaces p:commandButton

this sequence shall be as I have imagined.

However, in reality, the method

ajaxUploadFile()

is called BEFORE the input field with id saveCustomerId is updated.

Could you help me get the right sequence?

Here is the backing bean:

@ManagedBean
@ViewScoped
public class NewCustomerBean implements Serializable {


    public enum KundeTyp {

        TYP_NATPERS("Nat. Person"), TYP_FIRMA("Firma");

        private String value;

        private KundeTyp(String value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return value;
        }

    }

    private KundeTyp custmerType;
    private Map<String, KundeTyp> custmerTypes;

    private long savedKundeId;

    @Inject
    private KundeDBService kundeService;

    private String vorname;

    private String addresse;

    private String steuerNummer;

    private String kundeTyp = Integer.MIN_VALUE + "";

    @PostConstruct
    public void init() {
        custmerTypes = new HashMap<String, KundeTyp>();
        custmerTypes.put(KundeTyp.TYP_NATPERS.value, KundeTyp.TYP_NATPERS);
        custmerTypes.put(KundeTyp.TYP_FIRMA.value, KundeTyp.TYP_FIRMA);
    }

    public KundeTyp getCustmerType() {
        return custmerType;
    }

    public void setCustmerType(KundeTyp custmerType) {
        this.custmerType = custmerType;
    }

    public Map<String, KundeTyp> getCustmerTypes() {
        return custmerTypes;
    }

    public void setCustmerTypes(Map<String, KundeTyp> custmerTypes) {
        this.custmerTypes = custmerTypes;
    }

    public String getVorname() {
        return vorname;
    }

    public void setVorname(String vorname) {
        this.vorname = vorname;
    }

    public String getAddresse() {
        return addresse;
    }

    public void setAddresse(String addresse) {
        this.addresse = addresse;
    }

    public String getSteuerNummer() {
        return steuerNummer;
    }

    public void setSteuerNummer(String steuerNummer) {
        this.steuerNummer = steuerNummer;
    }

    public String getKundeTyp() {
        return kundeTyp;
    }

    public void setKundeTyp(String kundenTyp) {
        this.kundeTyp = kundenTyp;
    }

    public String saveNewCustomer(ActionEvent e) {


        Kunde neuerKunde = null;

        switch (this.custmerType) {
        case TYP_NATPERS: {
            neuerKunde = new NatuerlichePerson();
            break;
        }

        case TYP_FIRMA: {
            neuerKunde = new Firma();
            ((Firma) neuerKunde).setSteuerNummer("123456");
            break;
        }
        }

        neuerKunde.setVorname(vorname);
        neuerKunde.setAdresse(this.addresse);

        try {
            savedKundeId = kundeService.saveKunde(neuerKunde);

        } catch (ServiceException se) {

            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error",
                    "Unable to save the new customer: " + se.getMessage()));
        }

        return null;
    }

    public long getSavedKundeId() {
        return savedKundeId;
    }

    public void setSavedKundeId(long savedKundeId) {
        this.savedKundeId = savedKundeId;
    }
}
1
what if you remove type="submit"Kukeltje
@Kukeltje when I remove type = "submit" I am still having the same problemAlex Mi

1 Answers

0
votes

I would propose a work-around here, since I was not able to find a solution. Instead of updating the customerId on the front-end, we put it as a session attribute in the HttpSession.

Then, in the UploadServlet, which handles the file upload, we read this attribute and save the image under this customerId.