JSF:多次添加组件

时间:2012-05-11 18:29:27

标签: java jsf

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

每个表单中的字段以编程方式添加,每个字段根据潜在客户的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是会话作用域的吗?

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

    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;
}

0 个答案:

没有答案