简体   繁体   English

p:commandButton事件的执行顺序

[英]p:commandButton execution order of events

I am using PrimeFaces 6.0 components: 我正在使用PrimeFaces 6.0组件:

<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; 1.)在备用bean上执行actionListener方法以保存客户;

2.) Update the form field saveCustomerId with the id of the customer that is saved on step (1). 2.)用在步骤(1)中保存的客户的ID更新表单字段saveCustomerId。 The actionListener method generates a customer Id after the successful save and stores is as a bean property; 在成功保存和存储后,actionListener方法将生成一个客户ID,并将其作为bean属性存储。

3.) Execute the Java Script method ajaxUploadFile() 3.)执行Java Script方法ajaxUploadFile()

According to the link 根据链接

Execution order of events when pressing PrimeFaces p:commandButton 按下PrimeFaces p:commandButton时事件的执行顺序

this sequence shall be as I have imagined. 这个顺序应该是我想象的。

However, in reality, the method 但是实际上

ajaxUploadFile() ajaxUploadFile()

is called BEFORE the input field with id saveCustomerId is updated. 在更新ID为saveCustomerId的输入字段之前被调用。

Could you help me get the right sequence? 您能帮我按正确的顺序吗?

Here is the backing bean: 这是支持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;
    }
}

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. 我们没有在前端更新customerId,而是将其作为会话属性放在HttpSession中。

Then, in the UploadServlet, which handles the file upload, we read this attribute and save the image under this customerId. 然后,在处理文件上传的UploadServlet中,我们读取此属性并将图像保存在此customerId下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM