0

在我的应用程序中,我创建了一系列表单来编辑“潜在客户”的电子邮件地址。

每个表单中的字段都是以编程方式添加的,并且每个字段都会根据潜在客户的 ID 获得一个唯一的 ID。有 x 个潜在客户,因此调用此方法将每个潜在客户添加到视图中:

    public void addLead(HtmlPanelGrid grid, Lead lead, String beanName){
    updatedEmails.put(lead.getGuid(), lead.getEmail());
    wasAdded.put(lead.getGuid(), false);
    form = (HtmlForm)getApplication().createComponent(HtmlForm.COMPONENT_TYPE);
    form.setId("form"+lead.getGuid());
    grid.getChildren().add(form);

    HtmlOutputLabel label = (HtmlOutputLabel)getApplication().createComponent(HtmlOutputLabel.COMPONENT_TYPE);
    label.setId("ID"+lead.getGuid());
    label.setValue("Name: " + lead.getFirstName() +" " + lead.getLastName()+" Email: ");
    label.setStyleClass("hs-form label");
    form.getChildren().add(label);

    HtmlInputText inputText = (HtmlInputText)getApplication().createComponent(HtmlInputText.COMPONENT_TYPE);
    inputText.setId("input"+lead.getGuid());
    String binding = "#{" + beanName + "." + "updatedEmails[\""+lead.getGuid()+"\"]}";
    ValueExpression ve = getExpressionFactory().createValueExpression(getELContext(), binding, String.class);
    inputText.setValueExpression("value", ve);
    inputText.setStyleClass("hs-input");
    form.getChildren().add(inputText);

    HtmlCommandButton cmdButton = (HtmlCommandButton)getApplication().createComponent(HtmlCommandButton.COMPONENT_TYPE);
    cmdButton.setId("edit"+lead.getGuid());
    System.out.println("making edit button:" +lead.getGuid());
    String binding2  = "#{" + beanName + "." + "editEmail(\"" + lead.getGuid() + "\")}";
    System.err.println("binding:"+binding2);
    Class[] classList = new Class[1];
    classList[0] = String.class;
    MethodExpression ve2 = getExpressionFactory().createMethodExpression(getELContext(), binding2, null, classList);
    cmdButton.setActionExpression(ve2);
    cmdButton.setValue("Edit Email");
    cmdButton.setStyleClass("hs-button primary");
    System.out.println(cmdButton.toString());
    form.getChildren().add(cmdButton);

    HtmlCommandButton delButton = (HtmlCommandButton)getApplication().createComponent(HtmlCommandButton.COMPONENT_TYPE);
    delButton.setId("del"+lead.getGuid());
    String delBinding = "#{" + beanName + "." + "delLead(\"" + lead.getGuid() + "\")}";
    MethodExpression ve3 = getExpressionFactory().createMethodExpression(getELContext(), delBinding, null, classList);
    delButton.setActionExpression(ve3);
    delButton.setValue("Delete this Lead");
    delButton.setStyleClass("hs-button orange");
    form.getChildren().add(delButton);


}

我遇到的问题是,当调用 editEmail 方法时,调用将转到 API 进行编辑,并且另一个确认成功的 JSF 组件被添加到视图中。这第一次工作正常。问题是,如果之后您按下任何其他编辑按钮再次调用 editEmail,它会尝试添加新的成功组件并再次添加第一个。这当然会引发错误,因为它们具有相同的 ID。这是 editEmail 方法。

public void editEmail(String guid){
    System.out.println("Edit Email Called: "+guid);
    leadToEdit = leads.get(guid);
    leadToEdit.setEmail(updatedEmails.get(guid));
    System.out.println(leads.get(guid).getEmail());
    try {
        URL url;
        if(useOAuth){
            url = new URL("https://api.hubapi.com/leads/v1/lead/" + guid + "?access_token=" + portal.getOauthToken());
        }
        else
            url = new URL("https://api.hubapi.com/leads/v1/lead/"+guid+"?portalId="+portal.getPortalID()+"&hapikey="+portal.getApiKey());
        System.out.println(url.toString());
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        String request = "{\"email\":\""+leads.get(guid).getEmail()+"\"}";
        System.out.println(request);
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("PUT");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setUseCaches(false);

        DataOutputStream osw = new DataOutputStream(conn.getOutputStream());
        osw.writeBytes(request);
        osw.flush();

        System.err.println(conn.getResponseCode());
        if(conn.getResponseCode()<300)
        {
            UIViewRoot ui = FacesContext.getCurrentInstance().getViewRoot();
            HtmlForm form = (HtmlForm)ui.findComponent("mainForm:form"+leadToEdit.getGuid());
            final HtmlOutputText output = (HtmlOutputText)getApplication().createComponent(HtmlOutputText.COMPONENT_TYPE);
            output.setId("output"+leadToEdit.getGuid());
            output.setValue("Edit Success");
            output.setStyleClass("hs-block-message success");
            form.getChildren().add(output);
        }
        else{
            UIViewRoot ui = FacesContext.getCurrentInstance().getViewRoot();
            HtmlForm form = (HtmlForm)ui.findComponent("mainForm:form"+leadToEdit.getGuid());
            final HtmlOutputText output = (HtmlOutputText)getApplication().createComponent(HtmlOutputText.COMPONENT_TYPE);
            output.setId("output"+leadToEdit.getGuid());
            output.setValue("Edit Failure");
            output.setStyleClass("hs-block-message error");
            form.getChildren().add(output);
        }

        } 
    catch (Exception ex) {
        Logger.getLogger(PortalView.class.getName()).log(Level.SEVERE, null, ex);
    }
}

可能是因为 bean 是会话范围的吗?

addLead 在创建包含所有组件的 PanelGrid 时在此处调用:

    public HtmlPanelGrid getMainGrid(){
    if(mainGrid==null){
        mainGrid = (HtmlPanelGrid)getApplication().createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        mainGrid.setId("grid");
        for(Lead lead : leadArray){
            addLead(mainGrid, lead, "PortalView");
        }
    }
    return mainGrid;
}
4

0 回答 0