Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 2 在jsf2中创建动态输入_Jsf 2_Richfaces - Fatal编程技术网

Jsf 2 在jsf2中创建动态输入

Jsf 2 在jsf2中创建动态输入,jsf-2,richfaces,Jsf 2,Richfaces,我想在jsf2中的datatable中创建动态文本框,通过单击AddRow按钮创建文本框。我是jsf编程的新手。有人能告诉我一个动态生成的基本例子吗。我已经阅读了ui:repeat和c:foreach,但我需要一些实际的例子。对于您的示例 xhtml代码: <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:

我想在jsf2中的datatable中创建动态文本框,通过单击AddRow按钮创建文本框。我是jsf编程的新手。有人能告诉我一个动态生成的基本例子吗。我已经阅读了
ui:repeat
c:foreach
,但我需要一些实际的例子。

对于您的示例

xhtml代码:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core">
<h:head>
</h:head>
<h:body>
    <h:form  id="form1">
        <h:commandButton value="Add new Row" actionListener="#{myBean.addNewEmployee}"/>
        <h:dataTable value="#{myBean.employeeList}"  var="emp">
            <h:column>
                <f:facet name="header">
                    Employee Id
                </f:facet>
                #{emp.empId}
            </h:column>
            <h:column>
                <h:inputText value="#{emp.empName}"/>
            </h:column>
        </h:dataTable>
    </h:form>
</h:body>
</html>

您可以直接使用上述代码。我还建议在高级操作中使用PrimeFaces组件

这会在按钮单击时创建另一行inputtext吗?是,它正在使用inputtext添加新行
@ManagedBean
@ViewScoped
public class MyBean implements Serializable {

List<Employee> employeeList;

public List<Employee> getEmployeeList() {
    return employeeList;
}

public void setEmployeeList(List<Employee> employeeList) {
    this.employeeList = employeeList;
}

public void addNewEmployee(ActionEvent event) {
    employeeList.add(new Employee(employeeList.size(), null));
}

@PostConstruct
public void init() {
    employeeList = new ArrayList<Employee>();
    employeeList.add(new Employee(1, "Emp1"));
    employeeList.add(new Employee(2, "Emp2"));
}

public MyBean() {
}
}
public class Employee {

Integer empId;
String empName;

public Integer getEmpId() {
    return empId;
}

public void setEmpId(Integer empId) {
    this.empId = empId;
}

public String getEmpName() {
    return empName;
}

public void setEmpName(String empName) {
    this.empName = empName;
}

public Employee(Integer empId, String empName) {
    this.empId = empId;
    this.empName = empName;
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Employee other = (Employee) obj;
    if (this.empId != other.empId && (this.empId == null || !this.empId.equals(other.empId))) {
        return false;
    }
    return true;
}

@Override
public int hashCode() {
    int hash = 5;
    hash = 41 * hash + (this.empId != null ? this.empId.hashCode() : 0);
    return hash;
}
 }