简体   繁体   English

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

[英]Dynamically add and remove inputText field using primefaces

I have using primefaces in my project. 我在项目中使用了primefaces。 I need to create and delete field dynamically on my project. 我需要在我的项目中动态创建和删除字段。 Here I am going to attached my code please find it 在这里,我将附加我的代码,请找到它

my xhtml page is 我的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>

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






            }

And the Attachment Class is 附件类是

            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 am not able to add textBox by clicking the add button. 我无法通过单击添加按钮来添加textBox。 Please help. 请帮忙。 Thanks in advance. 提前致谢。

You need to put the whole in a <h:form> . 您需要将整体放在<h:form> See also commandButton/commandLink/ajax action/listener method not invoked or input value not updated . 另请参见commandButton / commandLink / ajax action / listener方法未调用或输入值未更新

Another major mistake is here: 另一个主要错误在这里:

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

Remove those binding attributes. 删除那些binding属性。 UI components are request scoped and yet you're binding them to a session scoped bean. UI组件是请求范围的,但是您将它们绑定到会话范围的bean。 This would fail hard if you open the same page in multiple browser windows in the same session. 如果您在同一会话中的多个浏览器窗口中打开同一页面,将很难失败。 See also How does the 'binding' attribute work in JSF? 另请参见“绑定”属性在JSF中如何工作? When and how should it be used? 什么时候以及如何使用? .

That said, the @ManagedBean @SessionScoped on AttachmentBean is completely unnecessary. 就是说, AttachmentBean上的@ManagedBean @SessionScoped完全没有必要。 Get rid of those annotations. 摆脱那些注释。 The @SessionScoped on UtilBean is also rather strange. UtilBean上的@SessionScoped也很奇怪。 The @ViewScoped is much better at its place here. @ViewScoped在这里的位置要好得多。 See also How to choose the right bean scope? 另请参见如何选择正确的bean作用域?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM