使用primefaces动态添加和删除inputText字段

时间:2014-08-19 05:38:39

标签: jsf jsf-2 primefaces

我在项目中使用了primefaces。我需要在项目中动态创建和删除字段。在这里,我将附上我的代码,请找到它

我的xhtml页面是

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

我的豆是:

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






            }

附件类是

            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();

                }


            }

我无法通过单击添加按钮添加textBox。请帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:0)

你需要将整体放在<h:form>中。另请参阅commandButton/commandLink/ajax action/listener method not invoked or input value not updated

另一个重大错误是:

<h:dataTable ... binding="#{utilBean.data}">
    ...
        <h:inputText ... binding="#{utilBean.attachment}"/>

删除这些binding属性。 UI组件是请求范围的,但您将它们绑定到会话范围的bean。如果您在同一会话中的多个浏览器窗口中打开同一页面,这将很难。另请参阅How does the 'binding' attribute work in JSF? When and how should it be used?

也就是说@ManagedBean @SessionScoped AttachmentBean完全没必要。摆脱那些注释。 @SessionScoped UtilBean也很奇怪。 @ViewScoped在这里的位置要好得多。另请参阅How to choose the right bean scope?