jsf - Dynamically add and remove inputText field using primefaces -


i have using primefaces in project. need create , delete field dynamically on project. here going attached code please find it

my xhtml page is

        <ui:composition xmlns="http://www.w3.org/1999/xhtml"         xmlns:ui="http://java.sun.com/jsf/facelets"         xmlns:h="http://java.sun.com/jsf/html"         xmlns:f="http://java.sun.com/jsf/core"         xmlns:p="http://primefaces.org/ui">              <h:panelgrid border="0" columns="3" cellpadding="4" columnclasses="control-label">                  <h:panelgrid columns="3"  cellpadding="4"  >                   <h:outputtext value="individual email"/>                 <div>                    <h:datatable value="#{utilbean.attachments}" var="attachmentbean" binding="#{utilbean.data}" id="attachments">                     <h:column>                         <h:inputtext id="attachment" value="#{attachmentbean.emailaddress}" binding="#{utilbean.attachment}"/>                     </h:column>                     <h:column>                         <h:commandbutton id="delete" value="delete" immediate="true" actionlistener="#{utilbean.deleteaddress}"/>                     </h:column>                 </h:datatable>                  <h:commandbutton id="add" value="add email address" immediate="true" actionlistener="#{utilbean.addaddress}" />                 </div>                 <br/>                </h:panelgrid>            </h:panelgrid>          </ui:composition> 

my bean is:

            import java.util.arraylist;             import java.util.list;              import javax.faces.bean.managedbean;             import javax.faces.bean.sessionscoped;             import javax.faces.component.uidata;             import javax.faces.component.uiinput;             import javax.faces.context.facescontext;             import javax.faces.event.actionevent;              @managedbean             @sessionscoped             public class utilbean{                  private uidata data=null;                 private uiinput attachment = null;                  private list<attachmentbean> attachments = new arraylist<attachmentbean>();                  public void addaddress(actionevent event){                      attachments.add(new attachmentbean());                     this.updateaddresses();                     facescontext.getcurrentinstance().renderresponse();                     system.out.println("adress size:" + attachments.size());                 }                  public void deleteaddress(actionevent event){                     int index = data.getrowindex();                          this.updateaddresses();                     this.getattachments().remove(index);                     facescontext.getcurrentinstance().renderresponse();                 }                  public void updateaddresses(){                     system.out.println("adress sizedfdfdfdf:" + attachments.size());                     @suppresswarnings("unchecked")                     list<attachmentbean> list = (arraylist<attachmentbean>)data.getvalue();                     for(int =0;i<data.getrowcount();i++){                         data.setrowindex(i);                         list.get(i).setemailaddress((string)getattachment().getsubmittedvalue());                     }                     data.setrowindex(0);                 }                  public uidata getdata() {                     return data;                 }                  public void setdata(uidata data) {                     this.data = data;                 }                   public uiinput getattachment() {                     return attachment;                 }                  public void setattachment(uiinput attachment) {                     this.attachment = attachment;                 }                  public list<attachmentbean> getattachments() {                     return attachments;                 }                  public void setattachments(list<attachmentbean> attachments) {                     this.attachments = attachments;                 }                   } 

and attachment class

            import javax.faces.bean.managedbean;             import javax.faces.bean.sessionscoped;              import java.io.serializable;              @managedbean             @sessionscoped             public class attachmentbean implements serializable {                  private static final long serialversionuid = 1l;                    private string emailaddress;                  public string getemailaddress() {                     return emailaddress;                 }                  public void setemailaddress(string emailaddress) {                     this.emailaddress = emailaddress;                 }                  public static long getserialversionuid() {                     return serialversionuid;                 }                  public attachmentbean() {                     super();                  }               } 

i not able add textbox clicking add button. please help. in advance.

you need put whole in <h:form>. see commandbutton/commandlink/ajax action/listener method not invoked or input value not updated.

another major mistake here:

<h:datatable ... binding="#{utilbean.data}">     ...         <h:inputtext ... binding="#{utilbean.attachment}"/> 

remove binding attributes. ui components request scoped , yet you're binding them session scoped bean. fail hard if open same page in multiple browser windows in same session. see how 'binding' attribute work in jsf? when , how should used?.

that said, @managedbean @sessionscoped on attachmentbean unnecessary. rid of annotations. @sessionscoped on utilbean rather strange. @viewscoped better @ place here. see how choose right bean scope?


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -