如何通过单击jsf中的添加按钮添加多个文本框

时间:2014-09-16 10:59:20

标签: spring jsf primefaces

我在jsf primefaces页面中添加新文本字段时会遇到问题。 PLZ,任何人都会帮助我它将如何做...主要的问题是,当我点击添加按钮时,整个页面都刷新它的确定,但是前一个文本框会自动删除并在前一个文本框上创建一个新文件。但我需要两个..例如,当我们在gmail中附加文件时,会显示我们附加的所有文件。我附加图像我需要的方式.. enter image description here

**.xhtml**
  <h:form >

        <h:panelGrid border="0" columns="3" cellpadding="4" columnClasses="control-label">

            <h:outputText value="Individual Email"/>
            <div>   
            <h:dataTable value="#{utilBean.addresses}" var="rec" binding="#{utilBean.data}"        id="addresses">
                <h:column>
                    <h:inputText id="address" value="#{rec.emailAddress}" binding="#{utilBean.address}"/>
                </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:form>                   

**utilBean.java**

 import java.util.ArrayList;
 import java.util.List;

 import javax.faces.bean.ManagedBean;
 import javax.faces.component.UIData;
 import javax.faces.component.UIInput;
 import javax.faces.context.FacesContext;
 import javax.faces.event.ActionEvent;

 @ManagedBean
 public class UtilBean{

 private UIData data=null;
 private UIInput address = null;

    private List<Address> addresses = new ArrayList<Address>();

  public void addAddress(ActionEvent event){

    addresses.add(new Address()); //add new textbox
    this.updateAddresses();
    FacesContext.getCurrentInstance().renderResponse();
    System.out.println("adress size:" + addresses.size());
  }

 public void deleteAddress(ActionEvent event){
    int index = data.getRowIndex();     
    this.updateAddresses();
    this.getAddresses().remove(index);
    FacesContext.getCurrentInstance().renderResponse();
 }

 public void updateAddresses(){ 
    List<Address> list = (ArrayList<Address>)data.getValue();
    for(int i =0;i<data.getRowCount();i++){
        data.setRowIndex(i);
        list.get(i).setEmailAddress((String)getAddress().getSubmittedValue() );
    }
    data.setRowIndex(0);
     }


  //Getters and Setters metod

   public UIData getData() {
    return data;
    }

  public void setData(UIData data) {
    this.data = data;
  }

  public UIInput getAddress() {
    return address;
  }

  public void setAddress(UIInput address) {
    this.address = address;
  }

  public List<Address> getAddresses() {
    return addresses;
  }

  public void setAddresses(List<Address> addresses) {
    this.addresses = addresses;
  }

}

 **Address.java**

 import java.io.Serializable;

import javax.faces.bean.ManagedBean;

@ManagedBean
public class Address implements Serializable {

private static final long serialVersionUID = 1L;

private String emailAddress;

//setter and getter method


 }

PLZ,任何人都会认出我的错误。

1 个答案:

答案 0 :(得分:0)

您必须在列表中添加绑定到p:datatable的新地址,并且只需更新p:datatable。它会工作。

<h:commandButton id="add" value="Add Email Address" immediate="true"   actionListener="#{utilBean.addAddress}" update="addresses"/>