Note that there are some explanatory texts on larger screens.

plurals
  1. POJSF: Component being added multiple times
    primarykey
    data
    text
    <p>In my application I create a series of forms for editing an email address for a 'lead'.</p> <p>The fields in each form are added programatically, and each gets a unique ID based on the ID of the lead. There are x number of leads, and so this method gets called to add each one to the view:</p> <pre><code> 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); } </code></pre> <p>The issue I am having is that when the editEmail method gets called the call goes out to the API to edit, and another JSF component to confirm success is added to the view. This works fine the first time. The issue is if after that you press any other edit button to call editEmail again, it tries to add the new success component AND the first one again. This of course throws an error since they have the same ID. Here is the editEmail method.</p> <pre><code>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()+"&amp;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()&lt;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); } } </code></pre> <p>Could it be because the bean is session scoped?</p> <p>addLead is called here when the PanelGrid that holds all the components is created:</p> <pre><code> 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload